【问题标题】:how to take a mean of a vector of doubles如何取双打向量的平均值
【发布时间】:2021-11-20 20:13:18
【问题描述】:
library(nycflights13)
flights = nycflights13::flights

flights %>% select(arr_delay, month) %>% group_by(month) %>% filter(!is.na(arr_delay))

我的目标是获取每个月的平均到达延迟,但每次我尝试取平均值时,都会出错

【问题讨论】:

    标签: r dplyr double mean tibble


    【解决方案1】:

    mean 中有一个na.rm 参数,所以不需要filter,而是在summarise 中使用mean

    library(dplyr)
    flights %>% 
        select(arr_delay, month) %>%
        group_by(month) %>% 
        summarise(Mean = mean(arr_delay, na.rm = TRUE))
    

    -输出

    # A tibble: 12 × 2
       month   Mean
       <int>  <dbl>
     1     1  6.13 
     2     2  5.61 
     3     3  5.81 
     4     4 11.2  
     5     5  3.52 
     6     6 16.5  
     7     7 16.7  
     8     8  6.04 
     9     9 -4.02 
    10    10 -0.167
    11    11  0.461
    12    12 14.9  
    

    【讨论】:

    • 非常感谢!效果很好
    猜你喜欢
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-09
    • 1970-01-01
    相关资源
    最近更新 更多