Edw*_*ard 8 aggregate r data.table
我在聚合数据框时遇到一些麻烦,同时保持组的原始顺序(基于数据框中的第一次出现的顺序).我已经设法做到了,但我希望有一个更简单的方法来解决它.
以下是要处理的示例数据集:
set.seed(7)
sel.1 <- sample(1:5, 20, replace = TRUE) # selection vector 1
sel.2 <- sample(1:5, 20, replace = TRUE)
add.1 <- sample(81:100) # additional vector 1
add.2 <- sample(81:100)
orig.df <- data.frame(sel.1, sel.2, add.1, add.2)
Run Code Online (Sandbox Code Playgroud)
需要注意的一些要点:有两个选择列可确定数据如何组合在一起.他们将是相同的,他们的名字是已知的.我在这些数据中只添加了两列,但可能还有更多.我给出了以'sel'和'add'开头的列名,以便更容易理解,但实际数据有不同的名称(因此虽然grep技巧很酷,但在这里它们没用).
我要做的是根据'sel'列将数据框聚合成组,并将所有'add'列加在一起.这很简单,使用aggregate如下:
# Get the names of all the additional columns
all.add <- names(orig.df)[!(names(orig.df)) %in% c("sel.1", "sel.2")]
aggr.df <- aggregate(orig.df[,all.add],
by=list(sel.1 = orig.df$sel.1, sel.2 = orig.df$sel.2), sum)
Run Code Online (Sandbox Code Playgroud)
问题是结果是由'sel'列排序的; 我希望它根据每个组首次出现在原始数据中进行排序.
以下是我做这项工作的最佳尝试:
## Attempt 1
# create indices for each row (x) and find the minimum index for each range
index.df <- aggregate(x = 1:nrow(orig.df),
by=list(sel.1 = orig.df$sel.1, sel.2 = orig.df$sel.2), min)
# Make sure the x vector (indices) are in the right range for aggr.df
index.order <- (1:nrow(index.df))[order(index.df$x)]
aggr.df[index.order,]
## Attempt 2
# get the unique groups. These are in the right order.
unique.sel <- unique(orig.df[,c("sel.1", "sel.2")])
# use sapply to effectively loop over data and sum additional columns.
sums <- t(sapply(1:nrow(unique.sel), function (x) {
sapply(all.add, function (y) {
sum(aggr.df[which(aggr.df$sel.1 == unique.sel$sel.1[x] &
aggr.df$sel.2 == unique.sel$sel.2[x]), y])
})
}))
data.frame(unique.sel, sums)
Run Code Online (Sandbox Code Playgroud)
虽然这些给了我正确的结果,但我希望有人可以指出一个更简单的解决方案.如果解决方案适用于标准R安装附带的软件包,那将是更好的选择.
我看过的文件aggregate和match,但我无法找到答案(我猜我希望这样的事情了"keep.original.order"参数aggregate).
任何帮助将非常感激!
更新:(万一有人偶然发现)
这是我尝试再过几天后能找到的最干净的方式:
unique(data.frame(sapply(names(orig.df), function(x){
if(x %in% c("sel.1", "sel.2")) orig.df[,x] else
ave(orig.df[,x], orig.df$sel.1, orig.df$sel.2, FUN=sum)},
simplify=FALSE)))
Run Code Online (Sandbox Code Playgroud)
它在data.table中简短而简单.默认情况下,它以第一个出现顺序返回组.
require(data.table)
DT = as.data.table(orig.df)
DT[, list(sum(add.1),sum(add.2)), by=list(sel.1,sel.2)]
sel.1 sel.2 V1 V2
1: 5 4 96 84
2: 2 2 175 176
3: 1 5 384 366
4: 2 5 95 89
5: 4 1 174 192
6: 2 4 82 87
7: 5 3 91 98
8: 3 2 189 178
9: 1 4 170 183
10: 1 1 100 91
11: 3 3 81 82
12: 5 5 83 88
13: 2 3 90 96
Run Code Online (Sandbox Code Playgroud)
这对于大数据来说速度很快,因此如果您确实发现速度问题,则无需在以后更改代码.以下替代语法是传递哪些列进行分组的最简单方法.
DT[, lapply(.SD,sum), by=c("sel.1","sel.2")]
sel.1 sel.2 add.1 add.2
1: 5 4 96 84
2: 2 2 175 176
3: 1 5 384 366
4: 2 5 95 89
5: 4 1 174 192
6: 2 4 82 87
7: 5 3 91 98
8: 3 2 189 178
9: 1 4 170 183
10: 1 1 100 91
11: 3 3 81 82
12: 5 5 83 88
13: 2 3 90 96
Run Code Online (Sandbox Code Playgroud)
或者,by也可以是一个逗号分隔的列名字符串:
DT[, lapply(.SD,sum), by="sel.1,sel.2"]
Run Code Online (Sandbox Code Playgroud)
读起来有点困难,但它给了你你想要的东西,我添加了一些评论来澄清。
# Define the columns you want to combine into the grouping variable
sel.col <- grepl("^sel", names(orig.df))
# Create the grouping variable
lev <- apply(orig.df[sel.col], 1, paste, collapse=" ")
# Split and sum up
data.frame(unique(orig.df[sel.col]),
t(sapply(split(orig.df[!sel.col], factor(lev, levels=unique(lev))),
apply, 2, sum)))
Run Code Online (Sandbox Code Playgroud)
输出看起来像这样
sel.1 sel.2 add.1 add.2
1 5 4 96 84
2 2 2 175 176
3 1 5 384 366
5 2 5 95 89
6 4 1 174 192
7 2 4 82 87
8 5 3 91 98
10 3 2 189 178
11 1 4 170 183
14 1 1 100 91
17 3 3 81 82
19 5 5 83 88
20 2 3 90 96
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6364 次 |
| 最近记录: |