如何使用dplyr或基数R计算链函数中的T / F观测数?

时间:2016-09-07 09:32:07

标签: r dplyr

假设我有一个名为pokemons的tbl_df:

      X.                  Name Type.1 Type.2 Total    HP Attack Defense Sp..Atk Sp..Def Speed Generation Legendary
    (int)                (fctr) (fctr) (fctr) (int) (int)  (int)   (int)   (int)   (int) (int)      (int)    (fctr)
 1     1             Bulbasaur  Grass Poison   318    45     49      49      65      65    45          1     False
 2     2               Ivysaur  Grass Poison   405    60     62      63      80      80    60          1     False
 3     3              Venusaur  Grass Poison   525    80     82      83     100     100    80          1     False
 4     3 VenusaurMega Venusaur  Grass Poison   625    80    100     123     122     120    80          1     False
 5     4            Charmander   Fire          309    39     52      43      60      50    65          1     False
 6     5            Charmeleon   Fire          405    58     64      58      80      65    80          1     False

我正在编写一个链函数(使用包dplyr)来总结数据集的重要统计数据,如下所示:

byType1 <- group_by(pokemons, Type.1)
summaryStats_byType1 <- summarise(byType1,
               count = n(),
               averageTotal = mean(Total, na.rm = T),
               medianGeneration = median(Generation, na.rm = T))

我应该怎样计算上面链函数中传说中的小精灵的数量?

1 个答案:

答案 0 :(得分:0)

我们可以做到

summaryStats_byType1  <-  pokemons %>%
                            group_by(Type.1) %>%
                            summarise(count = n(),
                                averageTotal = mean(Total, na.rm = T),
                                medianGeneration = median(Generation, na.rm = T),
                                CountLegendary = sum(as.character(Legendary)=="True"))