将数据框子集化为列表

时间:2012-03-12 17:00:22

标签: r

我有一个数据框,我想根据schoolid变量的唯一值进行子集化。然后将这些评分记录到列表中,因此列表的每个成员都是数据框的子集。我显然知道如何针对具体的schoolid值,一次一个地做这个,但是如何在不知道schoolid中可能出现哪些值的情况下系统地做到这一点?

感谢您的帮助! 杰夫

structure(list(schoolid = c(84351L, 84351L, 84351L, 84351L, 84351L, 
84351L, 84351L, 84351L, 84351L, 84351L, 84351L, 84351L, 84351L, 
84351L, 84351L, 84384L, 84384L, 84384L, 84384L, 84384L, 84384L, 
84384L, 84384L, 84384L, 84384L, 84384L, 84384L, 84384L, 84384L, 
84384L, 84385L, 84385L, 84385L, 84385L, 84385L, 84385L, 84385L, 
84385L, 84385L, 84385L, 84385L, 84385L, 84385L, 84385L, 84385L, 
84386L, 84386L, 84386L, 84386L, 84386L, 84386L, 84386L, 84386L, 
84386L, 84386L, 84386L, 84386L, 84386L, 84386L, 84386L, 84387L, 
84387L, 84387L, 84387L, 84387L, 84387L, 84387L, 84387L, 84387L, 
84388L, 84388L, 84388L, 84388L, 84388L, 84388L, 84388L, 84388L, 
84388L, 84389L, 84389L, 84389L, 84389L, 84389L, 84389L, 84389L, 
84389L, 84389L, 84395L, 84395L, 84395L, 84395L, 84395L, 84395L, 
84397L, 84397L, 84397L, 84397L, 84397L, 84397L, 84398L, 84398L, 
84398L, 84398L, 84398L, 84398L, 84351L, 84351L, 84351L, 84351L, 
84351L, 84351L, 84351L, 84351L, 84351L, 84351L, 84351L, 84351L, 
84384L, 84384L, 84384L, 84384L, 84384L, 84384L, 84384L, 84384L, 
84384L, 84384L, 84384L, 84385L, 84385L, 84385L, 84385L, 84385L, 
84385L, 84385L, 84385L, 84385L, 84385L, 84385L, 84385L, 84385L, 
84386L, 84386L, 84386L, 84386L, 84386L, 84386L, 84386L, 84386L, 
84386L, 84386L, 84386L, 84386L, 84387L, 84387L, 84387L, 84387L, 
84387L, 84387L, 84387L, 84387L, 84388L, 84388L, 84388L, 84388L, 
84388L, 84388L, 84389L, 84389L, 84389L, 84389L, 84389L, 84389L, 
84389L, 84389L, 84389L, 84397L, 84397L, 84398L, 84398L, 84398L, 
84398L, 84398L, 84351L, 84351L, 84351L, 84351L, 84351L, 84351L, 
84351L, 84351L, 84351L, 84351L, 84351L, 84351L, 84351L, 84351L, 
84384L, 84384L, 84384L, 84384L, 84384L, 84384L, 84384L, 84384L, 
84384L, 84384L, 84384L, 84384L, 84384L, 84385L, 84385L, 84385L, 
84385L, 84385L, 84385L, 84385L, 84385L, 84385L, 84385L, 84385L, 
84385L, 84385L, 84385L, 84385L, 84386L, 84386L, 84386L, 84386L, 
84386L, 84386L, 84386L, 84386L, 84386L, 84386L, 84386L, 84386L, 
84386L, 84386L, 84387L, 84387L, 84387L, 84387L, 84387L, 84387L, 
84387L, 84387L, 84388L, 84388L, 84388L, 84388L, 84388L, 84388L, 
84388L, 84388L, 84388L, 84389L, 84389L, 84389L, 84389L, 84389L, 
84389L, 84389L, 84395L, 84395L, 84395L, 84395L, 84397L, 84397L, 
84397L, 84397L, 84397L, 84397L, 84398L, 84398L, 84398L, 84398L, 
84398L, 84398L), subgroup = c("All", "All", "All", "All", "All", 
"All", "All", "All", "All", "All", "All", "All", "All", "All", 
"All", "All", "All", "All", "All", "All", "All", "All", "All", 
"All", "All", "All", "All", "All", "All", "All", "All", "All", 
"All", "All", "All", "All", "All", "All", "All", "All", "All", 
"All", "All", "All", "All", "All", "All", "All", "All", "All", 
"All", "All", "All", "All", "All", "All", "All", "All", "All", 
"All", "All", "All", "All", "All", "All", "All", "All", "All", 
"All", "All", "All", "All", "All", "All", "All", "All", "All", 
"All", "All", "All", "All", "All", "All", "All", "All", "All", 
"All", "All", "All", "All", "All", "All", "All", "All", "All", 
"All", "All", "All", "All", "All", "All", "All", "All", "All", 
"All", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", 
"ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", 
"ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", 
"ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", 
"ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", 
"ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", 
"ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", 
"ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", 
"ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "ELL", "SpEd", "SpEd", 
"SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", 
"SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", 
"SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", 
"SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", 
"SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", 
"SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", 
"SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", 
"SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", 
"SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", 
"SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", 
"SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd", 
"SpEd", "SpEd", "SpEd", "SpEd", "SpEd", "SpEd"), grade = c(0L, 
0L, 0L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 0L, 0L, 
0L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L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 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 3L, 1L, 2L, 3L, 
1L, 2L, 3L, 1L, 2L, 1L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
1L, 2L, 3L), .Label = c("AGL", "BGL", "OGL"), class = "factor"), 
    num = c(54L, 9L, 32L, 58L, 15L, 45L, 76L, 22L, 81L, 83L, 
    1L, 49L, 71L, 22L, 19L, 51L, 34L, 37L, 86L, 10L, 44L, 41L, 
    24L, 79L, 55L, 20L, 60L, 52L, 11L, 7L, 59L, 21L, 13L, 89L, 
    34L, 23L, 68L, 21L, 66L, 70L, 12L, 50L, 51L, 6L, 8L, 51L, 
    5L, 27L, 45L, 23L, 13L, 31L, 12L, 44L, 31L, 7L, 47L, 31L, 
    17L, 8L, 36L, 9L, 13L, 36L, 17L, 23L, 45L, 16L, 42L, 32L, 
    7L, 17L, 49L, 16L, 19L, 54L, 5L, 44L, 18L, 10L, 24L, 49L, 
    17L, 19L, 73L, 7L, 28L, 42L, 5L, 2L, 32L, 1L, 7L, 41L, 14L, 
    20L, 34L, 22L, 29L, 64L, 17L, 25L, 26L, 11L, 18L, 6L, 4L, 
    4L, 2L, 2L, 2L, 1L, 4L, 1L, 4L, 2L, 4L, 1L, 6L, 4L, 5L, 7L, 
    1L, 2L, 8L, 2L, 2L, 1L, 7L, 5L, 2L, 5L, 1L, 3L, 2L, 4L, 5L, 
    2L, 4L, 1L, 1L, 3L, 2L, 2L, 3L, 4L, 3L, 1L, 1L, 7L, 1L, 3L, 
    1L, 3L, 2L, 2L, 3L, 6L, 3L, 2L, 3L, 1L, 1L, 2L, 1L, 1L, 2L, 
    1L, 4L, 7L, 3L, 6L, 4L, 1L, 2L, 3L, 1L, 3L, 2L, 6L, 3L, 2L, 
    2L, 7L, 2L, 2L, 11L, 7L, 6L, 6L, 7L, 20L, 13L, 10L, 9L, 11L, 
    4L, 2L, 4L, 3L, 5L, 2L, 7L, 6L, 12L, 3L, 11L, 10L, 3L, 3L, 
    7L, 5L, 2L, 7L, 12L, 3L, 13L, 6L, 12L, 6L, 5L, 7L, 1L, 1L, 
    2L, 4L, 1L, 2L, 8L, 11L, 3L, 5L, 5L, 9L, 2L, 2L, 10L, 2L, 
    2L, 1L, 3L, 5L, 6L, 2L, 1L, 7L, 11L, 2L, 3L, 2L, 4L, 8L, 
    3L, 2L, 2L, 8L, 1L, 6L, 7L, 4L, 3L, 2L, 2L, 3L, 2L, 2L, 1L, 
    5L, 3L, 4L, 2L, 7L, 5L, 7L, 7L, 4L, 1L, 5L, 1L)), .Names = c("schoolid", 
"subgroup", "grade", "FnPLevel", "num"), row.names = c(NA, 279L
), class = "data.frame")

2 个答案:

答案 0 :(得分:6)

只需使用split

schoolid_groups <- split(Data, Data$schoolid)

答案 1 :(得分:2)

dlply包中查看plyrd = df输入,l =列表输出,ply = apply)

library(plyr) 

df <- data.frame(a=letters, b=rnorm(260))

out <- dlply(df, .(a))

str(out)