如何使用dplyr或基数R计算链函数中的T / F观测数? [英] How to count the number of T/F observations in chain functions using dplyr or base R?

查看:88
本文介绍了如何使用dplyr或基数R计算链函数中的T / F观测数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为 pokemons 的tbl_df,如下所示:

Suppose I have a tbl_df called pokemons like this:

      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 )来总结数据集的重要统计信息,如下所示:

I am writing a chain function (using the package dplyr) to summarize the important stats of the dataset, as followed:

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

我应该如何计算

推荐答案

我们可以做到

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"))

这篇关于如何使用dplyr或基数R计算链函数中的T / F观测数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆