我有一个名为的数据帧Cust_Amount,如下所示:
Age Amount_Spent
25 20
43 15
32 27
37 10
45 17
29 10
Run Code Online (Sandbox Code Playgroud)
我想将其划分为相同规模的年龄组,并将每个年龄组的花费总和如下:
Age_Group Total_Amount
20-30 30
30-40 37
40-50 32
Run Code Online (Sandbox Code Playgroud)
我们可以使用cut"年龄" sum分组并根据分组变量获取"Amount_Spent".
library(data.table)
setDT(df1)[,.(Total_Amount = sum(Amount_Spent)) ,
by = .(Age_Group = cut(Age, breaks = c(20, 30, 40, 50)))]
Run Code Online (Sandbox Code Playgroud)
或者 dplyr
library(dplyr)
df1 %>%
group_by(Age_Group = cut(Age, breaks = c(20, 30, 40, 50))) %>%
summarise(Total_Amount = sum(Amount_Spent))
# Age_Group Total_Amount
# <fctr> <int>
#1 (20,30] 30
#2 (30,40] 37
#3 (40,50] 32
Run Code Online (Sandbox Code Playgroud)