如何对列中的值进行求和,链接到另一列

时间:2017-04-02 13:50:31

标签: r

我有一个如下所示的数据集:

                                       Company Year                       Name   Position Gender     Sales     rdb rdbpersal Directors Executives Employees
1          Johnson & Johnson 2016          Dominic J. Caruso  Executive      0  71891000 9095000     12,65         9          9    126400
2          Johnson & Johnson 2016              Joaquin Duato  Executive      0  71891000 9095000     12,65         9          9    126400
3          Johnson & Johnson 2016            Peter M. Fasolo  Executive      0  71891000 9095000     12,65         9          9    126400
4          Johnson & Johnson 2015                Alex Gorsky  Executive      0  71891000 9095000     12,65         9          9    126400
5          Johnson & Johnson 2015             Jorge Mesquita  Executive      0  71891000 9095000     12,65         9          9    126400
6          Johnson & Johnson 2015         Sandra E. Peterson  Executive      1  71891000 9095000     12,65         9          9    126400
7          Johnson & Johnson 2015                Gary Pruden  Executive      0  71891000 9095000     12,65         9          9    126400
8          Johnson & Johnson 2014            Paulus Stoffels  Executive      0  71891000 9095000     12,65         9          9    126400
9          Johnson & Johnson 2014         Michael H. Ullmann  Executive      0  71891000 9095000     12,65         9          9    126400
1

我想描绘多年来男/女高管的人数。由于我只有一个虚拟变量,我想计算" 0"或" 1"每年和公司,并绘制它们。

我该如何做sum(data$Gender) "IF" (data$Year=same) "AND" (data$Company=same)之类的事情?

1 个答案:

答案 0 :(得分:1)

library(dplyr)

df %>% group_by(company,year) %>% summarise(n.gender = sum(gender))

您提供的数据只有一家公司。这将给你" 1"对于每个小组。