使用 pmap() 计算几列的行均值 [英] use pmap() to calculate row means of several columns

查看:69
本文介绍了使用 pmap() 计算几列的行均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更好地理解 pmap() 在数据帧中的工作方式,当应用 pmap() 计算来自多列的平均值时,我得到了一个令人惊讶的结果.

I'm trying to better understand how pmap() works within dataframes, and I get a surprising result when applying pmap() to compute means from several columns.

mtcars %>% 
  mutate(comp_var = pmap_dbl(list(vs, am, cyl), mean)) %>% 
  select(comp_var, vs, am, cyl)

在上面的例子中,comp_var 等于它所在行中 vs 的值,而不是给定行中三个变量的平均值.

In the above example, comp_var is equal to the value of vs in its row, rather than the mean of the three variables in a given row.

我知道我可以使用 ...

I know that I could get accurate results for comp_var using ...

mtcars %>% 
  rowwise() %>% 
    mutate(comp_var = mean(c(vs, am, cyl))) %>% 
    select(comp_var, vs, am, cyl) %>% 
  ungroup()

...但我想了解在这种情况下应该如何应用 pmap() .

... but I want to understand how pmap() should be applied in a case like this.

推荐答案

我们需要将 mean 中的 x 参数的参数连接为

We need to concatenate the argument for the x parameter in mean as

x:一个 R 对象.目前有数字/逻辑的方法向量和日期、日期时间和时间间隔对象.仅对trim = 0"允许复向量.

x: An R object. Currently there are methods for numeric/logical vectors and date, date-time and time interval objects. Complex vectors are allowed for ‘trim = 0’, only.

因此,如果我们传递 x1、x2、x3 等参数,它将根据使用情况进入 ... 参数

So, if we pass argument like x1, x2, x3, etc, it will be going into the ... parameter based on the usage

均值(x, ...)

例如

mean(5, 8) # x is 5
#[1] 5 
mean(8, 5) # x is 8
#[1] 8
mean(c(5, 8)) # x is a vector with 2 values
#[1] 6.5

rowwise 函数中,OP 将元素连接到单个向量,而使用 pmap 则保留为 mean 应用关于第一个参数

In the rowwise function, the OP concatenated the elements to a single vector while with pmap it is left as such for mean to apply on the first argument

out1 <- mtcars %>% 
         mutate(comp_var = pmap_dbl(list(vs, am, cyl), ~mean(c(...)))) %>% 
         dplyr::select(comp_var, vs, am, cyl)

-检查 rowwise 输出

out2 <- mtcars %>% 
         rowwise() %>% 
         mutate(comp_var = mean(c(vs, am, cyl))) %>% 
         dplyr::select(comp_var, vs, am, cyl) %>% 
         ungroup()

all.equal(out1, out2, check.attributes = FALSE)
#[1] TRUE

这篇关于使用 pmap() 计算几列的行均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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