从CSV有条件计数

时间:2019-03-14 00:32:15

标签: r csv

我有一个泰坦尼克号数据CSV,其中有几列不同的列描述了乘客。

我想找到既幸存下来又成年的乘客百分比。

如果查看我包含的数据,您将对我所说的有更好的了解。我似乎找不到任何与我的实际情况相符的内容。任何帮助将不胜感激。

这是数据:

     Class    Sex    Age  Survived
1      1st   Male  Child       Yes
2      1st   Male  Child       Yes
3      1st   Male  Child       Yes
4      1st   Male  Child       Yes
5      1st   Male  Child       Yes
6      1st   Male  Adult        No
7      1st   Male  Adult        No
8      1st   Male  Adult        No
9      1st   Male  Adult        No
10     1st   Male  Adult        No
11     1st   Male  Adult        No
12     1st   Male  Adult        No
13     1st   Male  Adult        No
14     1st   Male  Adult        No
15     1st   Male  Adult        No
16     1st   Male  Adult        No

1 个答案:

答案 0 :(得分:0)

这是dplyr的解决方案。最后一行将计算百分比。

library(dplyr)

live_adults <- mydata %>% 
  filter(Survived == "Yes", 
         Age == "Adult") %>% 
  nrow()

(live_adults/nrow(mydata))*100