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!

3
  • 3
    list2env(split(df, df$b), envir = .GlobalEnv) Commented Jul 7, 2017 at 0:02
  • 7
    It's often better to not split your data like this, but instead use tools like dplyr's group_by when you want to do separate calculations for each b group. Commented Jul 7, 2017 at 0:04
  • Thanks for all the help guys! Commented Jul 7, 2017 at 16:37

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

@d.b 's example is much more elegant IMO.
Thanks so much! I appreciate the two ways to go about it; i had never thought about using dplyr :|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.