0

For this dataframe:

df = data.frame(
  col = c("a","f","g","a")
)

How do I subset it for each unique letter and input it into a new dataframe like so?:

sheet_a <- subset(df, col == "a")
sheet_f <- subset(df, col == "f")
sheet_g <- subset(df, col == "g")

I think I need to use a column of unique characters using the below code in a for loop but I'm not sure how

uniq.name_col <- unique(as.vector(df$col))

Thank you for any help!

user553480
  • 321
  • 1
  • 8
  • 1
    A better plan might be to store the result in a list, which is easy to do with `split(df,df$col)`. If you're dead set on having them saved as objects, you could use `list2env(split(df,df$col),envir = globalenv())`. You would then have data.frames named `a`, `f`, and `g` in the global environment. – Ian Campbell Jul 03 '20 at 16:16

1 Answers1

1

You can try this, that includes code for exporting dataframes to environment:

#Create list
List <- split(df,df$col)
#Set to envir al dataframes
list2env(List,.GlobalEnv)
Duck
  • 39,058
  • 13
  • 42
  • 84