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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…