【问题标题】:keeping zero count with group_by in R在 R 中使用 group_by 保持零计数
【发布时间】:2020-03-04 10:15:06
【问题描述】:

几天来我一直在努力解决一个关于 group_by() 和 summarise() 的问题。我有类似这个数据集的营养数据:

library(tidyverse)

myData <- tibble(id = factor(c(rep("1", 5), rep("2", 4), rep("3", 6), rep("4", 5))),
                 gender = factor(c(rep("M", 5), rep("F", 4), rep("F", 6), rep("M", 5))),
                 age = c(rep("20-29", 5), rep("20-29", 4), rep("40-49", 6), rep("30-39", 5)),
                 bmi = c(rep("normal", 5), rep("normal", 4), rep("overweighted", 6), rep("underweighted", 5)),
                 food = factor(c("A", "A", "B", "C", "D", "D", "D", "A", "A", "B", "A", "B", "C", "C", "B", "D", "C", "E", "E", "A")),
                 food_class = factor(c("sweet", "sweet", "salty", "bitter", "acid", "acid", "acid", "sweet", "sweet",
                                "salty", "sweet", "salty", "bitter", "bitter", "salty", "acid", "bitter", 
                                "Other", "Other", "sweet")), 
                 quantity = c(25, 10, 15, 5, 15, 15, 30, 15, 5, 5, 10, 30, 15, 30, 10, 5, 5, 10, 15, 25))

myData %>%
  group_by(id,food, gender, food_class) %>%
  summarise(sum_quantity = sum(quantity)) %>%
  ungroup()%>%
  complete(id, food, food_class, fill = list(sum_quantity = 0))%>%
  group_by()

我得到的是:


# A tibble: 100 x 5
   id    food  food_class gender sum_quantity
   <fct> <fct> <fct>      <fct>         <dbl>
 1 1     A     acid       NA                0
 2 1     A     bitter     NA                0
 3 1     A     Other      NA                0
 4 1     A     salty      NA                0
 5 1     A     sweet      M                35
 6 1     B     acid       NA                0
 7 1     B     bitter     NA                0
 8 1     B     Other      NA                0
 9 1     B     salty      M                15
10 1     B     sweet      NA                0
# … with 90 more rows

我想分析我的数据集的营养数据,并通过对人们吃的数量求和来评估每个 food_class 的食物消耗。为此,我需要在平均计算中保持零计数,否则会出现偏差。但我也想保留性别或年龄组等信息,以便我可以确定每个性别、年龄等的食物消费模式。

使用 .drop = FALSE,我的变量组合会出现异常,因为每个 id 都会与两种性别结合,即使给定的 id 也有给定的性别。当我使用 complete() 时,我得到了很多 NA,这使分析变得复杂,因为我不能对值取决于性别或年龄的列使用填充参数。

关于如何解决我的问题的任何想法?非常感谢。

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    complete() 中使用nesting() 以保留出现在数据中的值组合。

    myData %>%
      group_by_at(vars(-quantity)) %>%
      summarise(sum_quantity = sum(quantity)) %>%
      ungroup %>%
      complete(nesting(id, gender, age, bmi),
               nesting(food, food_class),
               fill = list(sum_quantity = 0))
    
    # # A tibble: 20 x 7
    #    id    gender age   bmi           food  food_class sum_quantity
    #    <fct> <fct>  <chr> <chr>         <fct> <fct>             <dbl>
    #  1 1     M      20-29 normal        A     sweet                35
    #  2 1     M      20-29 normal        B     salty                15
    #  3 1     M      20-29 normal        C     bitter                5
    #  4 1     M      20-29 normal        D     acid                 15
    #  5 1     M      20-29 normal        E     Other                 0
    #  6 2     F      20-29 normal        A     sweet                20
    #  7 2     F      20-29 normal        B     salty                 0
    #  8 2     F      20-29 normal        C     bitter                0
    #  9 2     F      20-29 normal        D     acid                 45
    # 10 2     F      20-29 normal        E     Other                 0
    # 11 3     F      40-49 overweighted  A     sweet                10
    # 12 3     F      40-49 overweighted  B     salty                45
    # 13 3     F      40-49 overweighted  C     bitter               45
    # 14 3     F      40-49 overweighted  D     acid                  0
    # 15 3     F      40-49 overweighted  E     Other                 0
    # 16 4     M      30-39 underweighted A     sweet                25
    # 17 4     M      30-39 underweighted B     salty                 0
    # 18 4     M      30-39 underweighted C     bitter                5
    # 19 4     M      30-39 underweighted D     acid                  5
    # 20 4     M      30-39 underweighted E     Other                25
    

    【讨论】:

    • 非常感谢,这正是我想要的!
    猜你喜欢
    • 1970-01-01
    • 2017-03-11
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 2015-03-04
    相关资源
    最近更新 更多