我有一个这样的数据框:
data <- read.table(text="group; yr1; yr2; val
a; 1945; 1946; 20
a; 1945; 1946; 50
a; 1947; 1948; 40
b; 1926; 1927; 45
b; 1927; 1928; -10
b; 1927; 1928; -15 ", sep=";", header=T, stringsAsFactors = FALSE)
最好的方法是按小组总结每对年份的val
列,以便结果如下所示?
group yr1 yr2 val
a 1945 1946 70
a 1946 1947 40
b 1926 1927 45
b 1927 1928 -25
答案 0 :(得分:5)
在基地R:
aggregate(val ~ group + yr1 + yr2, data, sum)
# group yr1 yr2 val
#1 b 1926 1927 45
#2 b 1927 1928 -25
#3 a 1945 1946 70
#4 a 1947 1948 40
答案 1 :(得分:3)
尝试data.table
更大的数据集
library(data.table)
setDT(data)[, list(val=sum(val)), by=list(group, yr1, yr2)]
# group yr1 yr2 val
#1: a 1945 1946 70
#2: a 1947 1948 40
#3: b 1926 1927 45
#4: b 1927 1928 -25