aggregate
3つの列があると仮定して行うことができます。
aggregate(Amount~., df1, FUN=sum)
# Payer Period Amount
#1 1 1-1015 11
#2 3 1-1015 5
#3 2 2-1015 15
#4 3 3-1015 14
#5 1 4-1015 7
#6 3 4-1015 8
#7 1 5-1015 4
または
library(data.table)#v1.9.5+
setDT(df1)[, list(Amount=sum(Amount)), .(Period, Payer)]
# Period Payer Amount
#1: 1-1015 1 11
#2: 2-1015 2 15
#3: 3-1015 3 14
#4: 1-1015 3 5
#5: 4-1015 1 7
#6: 4-1015 3 8
#7: 5-1015 1 4
別の順序を使用する
aggregate(Amount~., df2, FUN=sum)
# Payer Period Amount
#1 1 1-1015 11
#2 3 1-1015 5
#3 2 2-1015 15
#4 3 3-1015 14
#5 1 4-1015 7
#6 3 4-1015 8
#7 1 5-1015 4
データ
df1 <- structure(list(Payer = c(1L, 2L, 3L, 1L, 3L, 1L, 3L, 1L),
Amount = c(10L,
15L, 14L, 1L, 5L, 7L, 8L, 4L), Period = c("1-1015", "2-1015",
"3-1015", "1-1015", "1-1015", "4-1015", "4-1015", "5-1015")),
.Names = c("Payer",
"Amount", "Period"), class = "data.frame", row.names = c(NA, -8L))
set.seed(24)
df2 <- df1[sample(nrow(df1)),]