我知道如何使用一个数据帧来工作和计算数学/统计数据。但是,当我不得不处理两个时会发生什么呢?例如:
> df1
supervisor salesperson
1 Supervisor1 Matt
2 Supervisor2 Amelia
3 Supervisor2 Philip
> df2
month channel Matt Amelia Philip
1 Jan Internet 10 50 20
2 Jan Cellphone 20 60 30
3 Feb Internet 40 40 30
4 Feb Cellphone 30 120 40如何以高效和通用的方式计算按渠道分组的主管的销售额?当您需要关联两个或更多数据帧以计算所需数据时,是否有任何方法或标准?
附言:这个数字是每个销售人员的销售额。
发布于 2018-05-10 22:37:19
下面是转换为long并使用tidyverse进行合并的想法,
library(tidyverse)
df2 %>%
gather(salesperson, val, -c(1:2)) %>%
left_join(., df1, by = 'salesperson') %>%
spread(salesperson, val, fill = 0) %>%
group_by(channel, supervisor) %>%
summarise_at(vars(names(.)[4:6]), funs(sum))这给了我们
A tibble: 4 x 5#组:频道主管阿米莉亚·马特·菲利普1手机Supervisor1 0.50。0。2手机Supervisor2 180.0。70.3互联网Supervisor1 0。50。0。4互联网Supervisor2 90。0。50.
注意:您还可以在group_by中添加month
https://stackoverflow.com/questions/50275207
复制相似问题