2017-04-02 55 views
-3

我有一個數據集,看起來像這樣:如何總結的列值,鏈接到另一列

         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
library(dplyr) 

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

您提供的數據組只有一個公司。這會給你每個組的「1」的總和。

+0

它的作品,非常感謝你! –

相關問題