I have a contingency table of meteorological stations and frequency of occurrence. I used logical indexing to create separate vectors like below (b1:b5) from the table. However there has to be a simpler way, perhaps from the apply family. Can someone provide such an example, thanks.
mf1<-c("USW00023047","USW00013966","USC00416740","USC00413828", "USC00414982", "USC00414982", "USW00013966", "USW00013966", "USW00003927",
"USW00003927", "USC00412019", "USC00411596", "USW00012960", "USW00012960", "USW00012960", "USW00012960", "USW00012960", "USC00417327",
"USC00417327", "USC00418433", "USC00417743", "USC00419499", "USC00419847", "USR0000TCLM", "USR0000TCOL", "USW00012921", "USW00012921",
"USW00012970", "USW00012921", "USW00012921", "USW00012924")
table(mf1)
dfcont<-as.data.frame(table(mf1))
a<-dfcont$mf1
b1<-a[dfcont$Freq < 6]
b2<-a[dfcont$Freq == 2]
b3<-a[dfcont$Freq == 3]
b4<-a[dfcont$Freq == 4]
b5<-a[dfcont$Freq == 5]
You can use split
:
temp <- split(as.character(dfcont$mf1), dfcont$Freq)
This will give you list of vectors in temp
. Usually, it is better to keep data in a list but if you want them as separate vectors assign name to them and use list2env
names(temp) <- paste0('b', seq_along(temp))
list2env(temp, .GlobalEnv)
You would now have b1
, b2
etc in your global environment.