尽管你没有制作一个可重复的例子来证明你的问题,但我认为你可以使用
ddply
函数来自
plyr
包,但您也可以使用基本函数
aggregate
.我更喜欢用
ddply公司
。
以下是一些用于计算数据的随机数。frame
#install.packages('plyr')
library(plyr)
set.seed(007) # for the example being reproducible
Mode_Cd1 <- replicate(4,sample(LETTERS[1:26], 1, replace=T)) # random genereation of variable Mode_Cd
Mode_Cd2 <- replicate(4,sample(LETTERS[1:26], 1, replace=T))
data_set <- data.frame(Trs_Id = rep(paste('00', 1:4, sep=''), each=3),
Mode_Cd = sample(paste(Mode_Cd1, Mode_Cd2, sep=''), 12, replace=T),
Service_Cd = sample(paste(Mode_Cd2, Mode_Cd1, sep=''), 12, replace=T),
Op_Sal_Wage_Amt = rnorm(12,5000,100),
Other_Sal_Wage = rnorm(12,3000,800))
data_set # this is how my random data_set looks like
Trs_Id Mode_Cd Service_Cd Op_Sal_Wage_Amt Other_Sal_Wage
1 001 ZG ID 4910.620 2213.558
2 001 KU UK 4969.267 2779.149
3 001 ZG ZB 4999.518 2303.319
4 002 ZG ZB 5098.816 3574.968
5 002 BZ ZB 5083.975 3088.522
6 002 ZG GZ 5070.534 2937.227
7 003 KU ID 5130.596 2663.608
8 003 ZG UK 4861.200 2550.299
9 003 DI ZB 5127.292 3798.011
10 004 ZG UK 5018.419 2115.896
11 004 BZ ID 5075.228 2886.170
12 004 KU UK 5059.175 3251.996
ddply(data_set,.(Trs_Id),numcolwise(sum)) # The sum you want.
Trs_Id Op_Sal_Wage_Amt Other_Sal_Wage
1 001 14879.40 7296.026
2 002 15253.33 9600.717
3 003 15119.09 9011.918
4 004 15152.82 8254.062