【问题标题】:Why does a tbf remain grouped after filling NAs with na.locf? [duplicate]为什么在用 na.locf 填充 NA 后 tbf 仍然分组? [复制]
【发布时间】:2019-11-12 19:02:48
【问题描述】:
library(dplyr)
library(zoo)

df_a <- iris %>%
    group_by(Species) %>%
    summarise(mean_petal_length = mean(Petal.Length))
sample_n(df_a, 2)

这会按预期返回 2 行随机汇总的 iris,尽管每个组只有一行 Species

但是,下面的另一个示例的行为似乎有所不同。

df_b <- iris %>%
    group_by(Species) %>%
    mutate(Petal.Length = na.locf(Petal.Length))
# Now df_b is the same with iris in terms of data contents
# since there's no missing vales in Petal.Length
sample_n(df_b, 60)

我希望得到 60 个随机行的 df_b,但这给了我一条错误消息:size 必须小于或等于 50(数据大小),设置 replace = TRUE 以使用带放回抽样

我可以看到这是因为每个组 Species 只有 50 行,在这种情况下,我必须在 mutate 之后 ungroup 才能获得预期的输出。我仍然不明白为什么会有这种差异。

【问题讨论】:

    标签: r random


    【解决方案1】:

    这与na.locf 无关,它与summarisemutategroup_by 的行为方式有关。让我试着用同样的例子来解释你。

    summarise 之后,分组丢失。检查

    library(dplyr)
    
    iris %>%
      group_by(Species) %>%
      summarise(mean_petal_length = mean(Petal.Length)) %>%
      mutate(n = n())
    
    # A tibble: 3 x 3
    #  Species    mean_petal_length     n
    #  <fct>                  <dbl> <int>
    #1 setosa                  1.46     3
    #2 versicolor              4.26     3
    #3 virginica               5.55     3
    

    如果SpeciesSpecies 分组,您会认为n 为1,但它显示为3,表示没有分组。

    因此,当您在汇总后执行 sample_n 时,它会从具有 3 行的总数据帧中采样并选择 2 个随机行。

    但是,mutate 的情况有所不同。

    iris %>%
      group_by(Species) %>%
      mutate(Petal.Length = mean(Petal.Length)) %>%
      mutate(n = n())
    
    # A tibble: 150 x 6
    # Groups:   Species [3]
    #   Sepal.Length Sepal.Width Petal.Length Petal.Width Species     n
    #          <dbl>       <dbl>        <dbl>       <dbl> <fct>   <int>
    # 1          5.1         3.5         1.46         0.2 setosa     50
    # 2          4.9         3           1.46         0.2 setosa     50
    # 3          4.7         3.2         1.46         0.2 setosa     50
    # 4          4.6         3.1         1.46         0.2 setosa     50
    # 5          5           3.6         1.46         0.2 setosa     50
    # 6          5.4         3.9         1.46         0.4 setosa     50
    # 7          4.6         3.4         1.46         0.3 setosa     50
    # 8          5           3.4         1.46         0.2 setosa     50
    # 9          4.4         2.9         1.46         0.2 setosa     50
    #10          4.9         3.1         1.46         0.1 setosa     50
    # … with 140 more rows
    

    分组仍然存在,它尝试从每个组中选择 60 行,而实际上它只有 50 行,因此出现错误。

    ?summarise 提及

    Value - 与 .data 属于同一类的对象。将删除一个分组级别。

    ?mutate 提到

    Value - 与 .data 同类的对象

    所以summarise 只删除了一层分组。以mtcars为例

    mtcars %>%
      group_by(cyl, am) %>%
      summarise(mean = mean(mpg))
    
    # A tibble: 6 x 3
    # Groups:   cyl [3]
    #    cyl    am  mean
    #  <dbl> <dbl> <dbl>
    #1     4     0  22.9
    #2     4     1  28.1
    #3     6     0  19.1
    #4     6     1  20.6
    #5     8     0  15.0
    #6     8     1  15.4
    

    仍按cyl分组,按am分组已丢失。

    【讨论】:

      【解决方案2】:

      从 group_by 文档中说:

      大多数数据操作都是在由变量定义的组上完成的。 group_by() 采用现有的 tbl 并将其转换为分组的 tbl,其中“按组”执行操作。 ungroup() 删除分组。

      因此,即使它看起来像同一个 tibble,你也必须将其视为 base 中的 split(iris, iris$Species)

      library(dplyr, quietly = TRUE, warn.conflicts = FALSE)
      
      df_b <- iris %>%
              group_by(Species)
      attributes(df_b)
      #> $names
      #> [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width" 
      #> [5] "Species"     
      #> 
      #> $class
      #> [1] "grouped_df" "tbl_df"     "tbl"        "data.frame"
      #> 
      #> $row.names
      #>   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
      #>  [18]  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34
      #>  [35]  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51
      #>  [52]  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68
      #>  [69]  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85
      #>  [86]  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102
      #> [103] 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
      #> [120] 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
      #> [137] 137 138 139 140 141 142 143 144 145 146 147 148 149 150
      #> 
      #> $groups
      #> # A tibble: 3 x 2
      #>   Species    .rows     
      #>   <fct>      <list>    
      #> 1 setosa     <int [50]>
      #> 2 versicolor <int [50]>
      #> 3 virginica  <int [50]>
      
      # equivalent in base R to:
      str( split(iris, iris$Species) )
      #> List of 3
      #>  $ setosa    :'data.frame':  50 obs. of  5 variables:
      #>   ..$ Sepal.Length: num [1:50] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
      #>   ..$ Sepal.Width : num [1:50] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
      #>   ..$ Petal.Length: num [1:50] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
      #>   ..$ Petal.Width : num [1:50] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
      #>   ..$ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
      #>  $ versicolor:'data.frame':  50 obs. of  5 variables:
      #>   ..$ Sepal.Length: num [1:50] 7 6.4 6.9 5.5 6.5 5.7 6.3 4.9 6.6 5.2 ...
      #>   ..$ Sepal.Width : num [1:50] 3.2 3.2 3.1 2.3 2.8 2.8 3.3 2.4 2.9 2.7 ...
      #>   ..$ Petal.Length: num [1:50] 4.7 4.5 4.9 4 4.6 4.5 4.7 3.3 4.6 3.9 ...
      #>   ..$ Petal.Width : num [1:50] 1.4 1.5 1.5 1.3 1.5 1.3 1.6 1 1.3 1.4 ...
      #>   ..$ Species     : Factor w/ 3 levels "setosa","versicolor",..: 2 2 2 2 2 2 2 2 2 2 ...
      #>  $ virginica :'data.frame':  50 obs. of  5 variables:
      #>   ..$ Sepal.Length: num [1:50] 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3 6.7 7.2 ...
      #>   ..$ Sepal.Width : num [1:50] 3.3 2.7 3 2.9 3 3 2.5 2.9 2.5 3.6 ...
      #>   ..$ Petal.Length: num [1:50] 6 5.1 5.9 5.6 5.8 6.6 4.5 6.3 5.8 6.1 ...
      #>   ..$ Petal.Width : num [1:50] 2.5 1.9 2.1 1.8 2.2 2.1 1.7 1.8 1.8 2.5 ...
      #>   ..$ Species     : Factor w/ 3 levels "setosa","versicolor",..: 3 3 3 3 3 3 3 3 3 3 ...
      
      # if you want to use a function not applied by group
      sample_n(df_b %>% ungroup() , 10)
      #> # A tibble: 10 x 5
      #>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species   
      #>           <dbl>       <dbl>        <dbl>       <dbl> <fct>     
      #>  1          7.7         3.8          6.7         2.2 virginica 
      #>  2          6.1         2.9          4.7         1.4 versicolor
      #>  3          5           3            1.6         0.2 setosa    
      #>  4          7.2         3.6          6.1         2.5 virginica 
      #>  5          6.8         2.8          4.8         1.4 versicolor
      #>  6          5.8         2.7          4.1         1   versicolor
      #>  7          6.6         3            4.4         1.4 versicolor
      #>  8          7.7         2.8          6.7         2   virginica 
      #>  9          5.1         3.5          1.4         0.2 setosa    
      #> 10          5           3.4          1.5         0.2 setosa
      

      【讨论】:

        猜你喜欢
        • 2016-12-13
        • 1970-01-01
        • 2012-09-09
        • 1970-01-01
        • 2014-08-03
        • 2017-05-12
        • 2015-02-18
        • 1970-01-01
        • 2012-12-03
        相关资源
        最近更新 更多