【问题标题】:Getting mean of columns that have missing (NA) values获取具有缺失 (NA) 值的列的平均值
【发布时间】:2019-12-15 02:56:52
【问题描述】:

我有一个包含 6 年数据的数据框。每一年都有相同的变量。我试图找到 6 年内每个变量的平均值。每年都有不同行的缺失(NA)。在这个例子中,我试图获得 6 岁以上女孩人数的平均值。

我尝试过使用 mutate 和 pipe 函数,但它似乎不起作用。通过复制我的所有列,它给了我奇怪的结果。

roughcopy2$headcount_girls_mean <- 

  roughcopy2 %>% 
  mutate(headcount_girls_mean=rowMeans(.[ , 
c("headcount_total_girls_rounded_1314","headcount_total_girls_rounded_1415", 
                               "headcount_total_girls_rounded_1516" , 
"headcount_total_girls_rounded_1617",
                               "headcount_total_girls_1718", 
"headcount_total_girls_1819")], na.rm=TRUE))

此代码复制了我的所有数据框列,并添加了“headcount_girls_mean”。复制数据集中的每个列名。所以我的原始数据集,即 roughcopy2 有 150 列。运行上述内容后,我得到 300 列,后 150 列与前 150 列标题相同,但前缀为“headcount_girls_mean”。

【问题讨论】:

  • 请使用dput 添加您的数据样本。使用dput(head(roughcopy,n))
  • 您应该添加@NelsonGon 已经提到的数据样本(或数据的表示形式)。从您告诉我们的内容来看,我相信您应该使用tidyr 库中的gather() 之类的东西。使用gather(),您可以将列折叠成键值对,然后对这个“整洁”的数据框进行分析。
  • 尝试在dplyrmutate 上阅读本教程。 stat545.com/block010_dplyr-end-single-table.html 我不想听起来很纯粹,但你使用 mutate 非常错误。

标签: r dplyr


【解决方案1】:

使用您的数据框的假想样本:

roughcopy2 <- data.frame("headcount_total_girls_rounded_1314"=c(1,4,2,4,8),
                          "headcount_total_girls_rounded_1415"=c(2, NA, 4, NA,8),
                          "headcount_total_girls_rounded_1516"=c(6,8,10,12,14),
                          "headcount_total_girls_rounded_1617"=c(4,5,5,3,2),
                          "headcount_total_girls_1718"=c(8,5,9,NA,2),
                          "headcount_total_girls_1819"=c(NA,2,4,7,3))

如果你想要列的平均值,你可以简单地:

means <- as.numeric(colMeans(x=roughcopy2, na.rm = TRUE))

但是,如果您想要跨多个列值的平均值:

roughcopy2 <- mutate(roughcopy2,
                     headcount_mean = rowMeans(select(roughcopy2, starts_with("headcount")),
                     na.rm = TRUE))

它应该输出(其他列被省略,但它们在数据框中):

  headcount_total_girls_1718 headcount_total_girls_1819 head_count_mean
1                          8                         NA            4.20
2                          5                          2            4.80
3                          9                          4            5.67
4                         NA                          7            6.50
5                          2                          3            6.17

您应该放一个原始数据框的样本,并大致了解您期望的输出结果。

【讨论】:

    【解决方案2】:

    我仍然不确定您的意图,但如果您想要每列的平均值,以下应该可行。这个答案基于我的评论和@Pedro_Henrique:

    library(tidyverse)
    
    roughcopy2 <- tibble("headcount_total_girls_rounded_1314"=c(1,4,2,4,8),
                              "headcount_total_girls_rounded_1415"=c(2, NA, 4, NA,8),
                              "headcount_total_girls_rounded_1516"=c(6,8,10,12,14),
                              "headcount_total_girls_rounded_1617"=c(4,5,5,3,2),
                              "headcount_total_girls_1718"=c(8,5,9,NA,2),
                              "headcount_total_girls_1819"=c(NA,2,4,7,3))
    
    roughcopy2 %>%
      gather(headcount_year, count) %>%
      group_by(headcount_year) %>%
      summarise(mean_count = mean(count, na.rm = TRUE))
    

    结果输出:

    # A tibble: 6 x 2
      headcount_year                     mean_count
      <chr>                                   <dbl>
    1 headcount_total_girls_1718               6   
    2 headcount_total_girls_1819               4   
    3 headcount_total_girls_rounded_1314       3.8 
    4 headcount_total_girls_rounded_1415       4.67
    5 headcount_total_girls_rounded_1516      10   
    6 headcount_total_girls_rounded_1617       3.8
    

    【讨论】:

      猜你喜欢
      • 2017-01-03
      • 2016-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多