我有一个数据集(测试)
v1 v2 v3 v4 v5 v6
1 1 1 0 0 0
2 2 1 1 0 0
3 2 1 0 0 0
4 3 1 0 0 0
5 3 1 1 0 1
6 3 1 0 1 1
structure(list(V1 = 1:6, V2 = c(1L, 2L, 2L, 3L, 3L, 3L), V3 = c(1L,
1L, 1L, 1L, 1L, 1L), V4 = c(0L, 1L, 0L, 0L, 1L, 0L), V5 = c(0L,
0L, 0L, 0L, 0L, 1L), V6 = c(0L, 0L, 0L, 0L, 1L, 1L)), .Names = c("V1",
"V2", "V3", "V4", "V5", "V6"), class = "data.frame", row.names = c(NA,
-6L))
我想实现这个
v1 v2 v3 v4 v5 v6
1 1 1 0 0 0
5 2 2 1 0 0
15 3 3 1 1 2
我试过这个:
aggregate(test[c('v3', 'v4', 'v5','v6')], list('v2'), FUN=sum, na.rm=TRUE)
这是行不通的。我想基于 V2 聚合(测试)中的数据并对其他变量求和。