如何在 R 数据帧的因子级别中按各个列求和?
如果数据帧包含一个因子列和一些数值列,则我们可能希望查找因子级别的数值列的总和。为此,我们可以使用 aggregate 函数。例如,如果我们有一个数据帧 df,其中包含由 Group 定义的因子列和一些数值列,则可以通过使用 aggregate(.~Group,data=df,sum) 计算因子级别的按各个列求和。
示例 1
考虑以下数据帧 −
Group<−factor(sample(c("A","B","C"),20,replace=TRUE)) frequency<−sample(1:10,20,replace=TRUE) cost<−round(rnorm(20,25,6),2) df1<−data.frame(Group,frequency,cost) df1
输出
Group frequency cost 1 A 6 21.69 2 C 5 34.94 3 C 3 17.32 4 B 3 16.84 5 A 10 23.10 6 C 3 30.30 7 B 8 19.84 8 A 1 25.41 9 C 2 27.55 10 A 10 26.31 11 B 7 33.05 12 A 10 32.09 13 B 1 27.36 14 A 9 19.70 15 A 5 26.44 16 A 10 28.28 17 C 6 25.67 18 A 9 24.06 19 C 3 22.25 20 A 5 24.93
寻找 frequency 和 cost 在 Group 中的水平和 −
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
示例
aggregate(.~Group,data=df1,sum) Group frequency cost
输出
1 A 75 252.01 2 B 19 97.09 3 C 22 158.03
示例 2
Class<−sample(c("First","Second","Third"),20,replace=TRUE) Price<−sample(2000:5000,20) Seats<−sample(0:9,20,replace=TRUE) df2<−data.frame(Class,Price,Seats) df2
输出
Class Price Seats 1 Third 2218 4 2 Second 3064 4 3 Third 4074 2 4 First 4394 4 5 First 2321 3 6 Third 4998 1 7 First 3520 2 8 First 4133 1 9 Third 4832 9 10 Second 2856 0 11 Third 3145 7 12 Third 4604 6 13 Second 4691 9 14 First 4994 4 15 Third 2252 2 16 First 3491 0 17 Second 4125 7 18 Second 2597 2 19 Third 3720 3 20 Second 2995 0
寻找 Price 和 Seats 在 Class 中的水平和 −
示例
aggregate(.~Class,data=df2,sum)
输出
Class Price Seats 1 First 22853 14 2 Second 20328 22 3 Third 29843 34
广告