使用 ftransform 和来自折叠 R 包的 fgroup_by [英] Using ftransform along with fgroup_by from collapse R package

查看:41
本文介绍了使用 ftransform 和来自折叠 R 包的 fgroup_by的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Rcollapse 重现 dplyr 代码的以下输出.

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

dplyr 代码

dplyr Code

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

dplyr 代码输出

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 代码输出

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

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

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

推荐答案

fmean 默认使用 na.rm = TRUE.此外,还有一个选项可以指定 fmean 内的分组,即 g.默认情况下,TRANULL 并返回一个汇总输出,但我们可以将其更改为 replace_fill 以返回完整长度

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'))

-输出

# 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或数据上的分组变量(.)

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'))

这篇关于使用 ftransform 和来自折叠 R 包的 fgroup_by的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆