使用 ftransform 以及来自 collapse R 包的 fgroup_by

Using ftransform along with fgroup_by from collapse R package

我正在尝试使用 Rcollapse.

重现 dplyr 代码的以下输出

dplyr代码

library(tidyverse)
starwars %>%
  select(name, mass, species) %>%
  group_by(species) %>%
  mutate(mass_norm = mean(mass, na.rm = TRUE))

dplyr 代码输出

# A tibble: 87 x 4
# Groups:   species [38]
   name                mass species mass_norm
   <chr>              <dbl> <chr>       <dbl>
 1 Luke Skywalker        77 Human        82.8
 2 C-3PO                 75 Droid        69.8
 3 R2-D2                 32 Droid        69.8
 4 Darth Vader          136 Human        82.8
 5 Leia Organa           49 Human        82.8
 6 Owen Lars            120 Human        82.8
 7 Beru Whitesun lars    75 Human        82.8
 8 R5-D4                 32 Droid        69.8
 9 Biggs Darklighter     84 Human        82.8
10 Obi-Wan Kenobi        77 Human        82.8
# … with 77 more rows

collapse代码

library(collapse)
starwars %>%
  fselect(name, mass, species) %>%
  fgroup_by(species) %>%
  ftransform(mass_norm = fmean(mass, na.rm = TRUE))

collapse 代码输出

# A tibble: 87 x 4
   name                mass species mass_norm
 * <chr>              <dbl> <chr>       <dbl>
 1 Luke Skywalker        77 Human        97.3
 2 C-3PO                 75 Droid        97.3
 3 R2-D2                 32 Droid        97.3
 4 Darth Vader          136 Human        97.3
 5 Leia Organa           49 Human        97.3
 6 Owen Lars            120 Human        97.3
 7 Beru Whitesun lars    75 Human        97.3
 8 R5-D4                 32 Droid        97.3
 9 Biggs Darklighter     84 Human        97.3
10 Obi-Wan Kenobi        77 Human        97.3
# … with 77 more rows

Grouped by:  species  [38 | 2 (5.5)] 

想知道为什么我用 collapse 代码得到错误答案。任何提示。

fmean默认使用na.rm = TRUE。此外,还有一个选项可以在 fmean 内指定分组,即 g。默认情况下,TRANULL,它return是一个汇总输出,但是我们可以把它改成replace_fill到return全长

library(collapse)
ftransform(slt(starwars, name, mass, species),
      mass_norm = fmean(mass, species, TRA = 'replace_fill'))

-输出

# A tibble: 87 x 4
#   name                mass species mass_norm
# * <chr>              <dbl> <chr>       <dbl>
# 1 Luke Skywalker        77 Human        82.8
# 2 C-3PO                 75 Droid        69.8
# 3 R2-D2                 32 Droid        69.8
# 4 Darth Vader          136 Human        82.8
# 5 Leia Organa           49 Human        82.8
# 6 Owen Lars            120 Human        82.8
# 7 Beru Whitesun lars    75 Human        82.8
# 8 R5-D4                 32 Droid        69.8
# 9 Biggs Darklighter     84 Human        82.8
#10 Obi-Wan Kenobi        77 Human        82.8
# … with 77 more rows

如果我们要使用链,使用GRP指定数据上的g或分组变量(.)

library(dplyr)
starwars %>%
 fselect(name, mass, species) %>%
 fgroup_by(species) %>%
 ftransform(mass_norm = fmean(mass, GRP(.), TRA = 'replace'))