0

Quick question for you. I have the following:

a <- c(1,5,2,3,4,5,3,2,1,3)
b <- c("a","a","f","d","f","c","a","r","a","c")
c <- c(.2,.6,.4,.545,.98,.312,.112,.4,.9,.5)
df <- data.frame(a,b,c)

What i am looking to do is utilize a for loop to create multiple data frames from rows based on the column contents of column B (i.e. a df for the "a," the "d," and so on).

At the same time, I would also like to name the data frame based on the corresponding value from column B (df will be named "a" for the data frame created from the "a."

I tried making it work based off the answers provided here Using a loop to create multiple data frames in R but i had no luck.

If it helps, I have variables created with levels() and nlevels() to use in the loop to keep it scalable based on how my data changes. Any help would be much appreciated.

Thanks!

1 Answers1

3

This should do:

require(dplyr)

df$b <- as.character(df$b)

col.filters <- unique(df$b) 

lapply(seq_along(col.filters), function(x) {
  filter(df, b == col.filters[x])
  }
  ) -> list

names(list) <- col.filters

list2env(list, .GlobalEnv)

Naturally, you don't need dplyr to do this. You can just use base syntax:

df$b <- as.character(df$b)

col.filters <- unique(df$b) 

lapply(seq_along(col.filters), function(x) {
  df[df[, "b"] == col.filters[x], ]
  }
  ) -> list

names(list) <- col.filters

list2env(list, .GlobalEnv)

But I find dplyrmuch more intuitive.

Cheers

JdeMello
  • 1,708
  • 15
  • 23