我正在查看NBA统计数据,尤其是每场得分和薪水。当前使用dplyr
的group_by函数在R中对其进行汇总。我试图将PPG分组,以查看每场比赛得分均达到特定范围的球员的薪水。目前,它仅列出了我数据中的每个PPG值,有没有办法将其变成一个范围。例如0-10 PPG,10-20 PPG,20-30 PPG和30 +。
这是我目前拥有的代码行:
cps3 <- cps1 %>% group_by(PPG) %>%
summarize(Mean_Salary = mean(Salary),
Min_Salary = min(Salary),
Max_Salary = max(Salary),
Number_of_Salaries = n())
cps1
只是我的数据集。
答案 0 :(得分:1)
您只需要为组创建新变量并将其用作分组变量即可。
cps1 %>%
mutate(PPG_tenths = floor(PPG/10) ) %>%
group_by(PPG_tenths ) %>%
summarize(
Mean_Salary = mean(Salary),
in_Salary = min(Salary),
Max_Salary = max(Salary),
Number_of_Salaries = n()
)