假设我有一个亚利桑那州第10类学生的数据框(名称:“学生”),其中包含 ID号,头发颜色,眼睛颜色和重量列。有5种头发颜色和4种眼睛颜色。头发颜色离散(1-5),眼睛颜色(1-4)
我想制作20个名为“x_Students_y”的新子集/数据框,其中x_Students_y表示具有头发颜色x(1-5中的任何一个)和眼睛颜色y(任何外出的颜色)的学生的数据框/子集1-4)。我们怎么做呢?
(注意,Discretization(例如):头发颜色:1:黑色,2:蓝色,3:棕色,4:红色,5:灰色)
答案 0 :(得分:1)
使用基数R:
df <- data.frame(hair=sample(4,20,replace=T),
eyes=sample(letters[1:4],20,replace=T),
foo=rnorm(20),
bar=runif(20))
frames <- split(df,paste0(df$eyes,'_students_',df$hair))
# and in case you want the data.frames defined in the local environment
list2env(frames,as.environment(-1))