假设我有这个 R 数据框:
ts year month day
1 1295234818000 2011 1 17
2 1295234834000 2011 1 17
3 1295248650000 2011 1 17
4 1295775095000 2011 1 23
5 1296014022000 2011 1 26
6 1296098704000 2011 1 27
7 1296528979000 2011 2 1
8 1296528987000 2011 2 1
9 1297037448000 2011 2 7
10 1297037463000 2011 2 7
dput(a)
structure(list(ts = c(1295234818000, 1295234834000, 1295248650000,
1295775095000, 1296014022000, 1296098704000, 1296528979000, 1296528987000,
1297037448000, 1297037463000), year = c(2011, 2011, 2011, 2011,
2011, 2011, 2011, 2011, 2011, 2011), month = c(1, 1, 1, 1, 1,
1, 2, 2, 2, 2), day = c(17, 17, 17, 23, 26, 27, 1, 1, 7, 7)), .Names = c("ts",
"year", "month", "day"), row.names = c(NA, 10L), class = "data.frame")
有没有一种方法可以创建一个数据框向量,其中每个数据框都是原始数据框的一个子集,具有独特的年月日分组组合?理想情况下,我想按顺序取回数据帧 DF1、DF2、DF3、DF4、DF5 和 DF6,其中:
DF1:
ts year month day
1 1295234818000 2011 1 17
2 1295234834000 2011 1 17
3 1295248650000 2011 1 17
DF2:
4 1295775095000 2011 1 23
DF3:
5 1296014022000 2011 1 26
DF4:
6 1296098704000 2011 1 27
DF5:
7 1296528979000 2011 2 1
8 1296528987000 2011 2 1
DF6:
9 1297037448000 2011 2 7
10 1297037463000 2011 2 7
如有任何帮助,我们将不胜感激。
最佳答案
df <- df[order(df$year, df$month, df$day), ]
df.list <- split(df, list(df$year, df$month, df$day), drop=TRUE)
listnames <- setNames(paste0("DF", 1:length(df.list)), sort(names(df.list)))
names(df.list) <- listnames[names(df.list)]
list2env(df.list, envir=globalenv())
# > DF1
# ts year month day
# 1 1.295235e+12 2011 1 17
# 2 1.295235e+12 2011 1 17
# 3 1.295249e+12 2011 1 17
# > DF6
# ts year month day
# 9 1.297037e+12 2011 2 7
# 10 1.297037e+12 2011 2 7
编辑:
正如@thelatemail 所建议的,通过在 split
中正确排序可以更容易地实现相同的目标:
df.list <- with(df, split(df, list(day,month,year), drop=TRUE))
df.list <- setNames(df.list, paste0("DF",seq_along(df.list)))
list2env(df.list, envir=globalenv())
关于r - 基于列的分组创建数据帧子集的向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21894906/