1

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]
DAY
  • 91
  • 6

2 Answers2

1

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.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

I couldn't find anything simpler than

tbl <- table(mf1)
split(names(tbl), tbl)

If the names need to be b*, assign by pasting the "b" as a prefix to the current names.

names(sp) <- paste0('b', names(sp))
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66