Using ftransform along with fgroup_by from collapse R package

Viewed 118

I'm trying to reproduce the following output of dplyr code with R package collapse.

dplyr Code

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

dplyr Code Output

# 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 Code

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

collapse Code Output

# 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)] 

Wondering why I am getting wrong answer with collapse code. Any hints.

1 Answers

The fmean by default uses na.rm = TRUE. Also, there is an option to specify the grouping within fmean i.e. g. By default, TRA is NULL and it returns a summarised output, but we can change it to replace_fill to return the full length

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

-output

# 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

If we want to use the chain, use GRP to specify the g or grouping variable on the data (.)

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