I want to split an existing dataframe by the levels of one of the factor variables so that the names of the split dataframes would correspond to the levels of the factor.
df <- data.frame(cbind(X = 1:10, Y = rnorm(10)), Z = sample(LETTERS[1:3], 10, replace = TRUE))
If df
is the original dataframe, I want to split it into three dataframes called A
, B
and C
, such that:
A = subset(df, Z == 'A')
B = subset(df, Z == 'B')
...
Is there an easy way to do this in one shot? I have a huge dataset and the factor variable has too many levels.
df %>% group_by(Z) %>% summarize/mutate(... other code) ... %>% ungroup()
– smci