了解rowwise()和c_across() [英] Understanding rowwise() and c_across()

查看:63
本文介绍了了解rowwise()和c_across()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好:有人能为这两种尝试计算分数的行平均值的方法为何工作方式不同的人提供非专业人士的解释吗?谢谢.

Hi there: Can anyone offer a layperson's explanation for why these two ways of trying to calculate an row average of scores work differently? Thanks.

library(tidyverse)
var1<-rnorm(100)
var2<-rnorm(100)
var3<-rnorm(100)

df<-data.frame(var1, var2, var3)

#ADD IN A MISSING VALUE
df[1,1]<-NA

#I thought this would work
df %>% 
  select(starts_with('var')) %>% 
  rowwise() %>% 
  mutate(avg=mean(., na.rm=T))
#This does work but I don't understand why
df %>% 
  rowwise() %>% 
  mutate(avg=
           mean(
             c_across(starts_with('var')), na.rm=T)
         )

推荐答案

  • .代表整个数据集,而不是分组的数据.
  • 此外,平均值不适用于数据框.(请参见平均值(mtcars))
    • . represents the entire dataset and not the grouped data.
    • Moreover, mean doesn't work on dataframes. (see mean(mtcars))
    • 由于 dplyr 1.0.0(或更高版本),您可以使用 cur_data()获取组中的数据,但可以使用 mean 您需要将其更改为vector,可以使用 unlist as.matrix 完成.所以尝试:

      Since dplyr 1.0.0 (or higher) you can use cur_data() to get data in the group but to use it in mean you need to change it to vector which can be done with unlist or as.matrix. So try :

      library(dplyr)
      
      df %>% 
        select(starts_with('var')) %>% 
        rowwise() %>% 
        mutate(avg=mean(unlist(cur_data()), na.rm=T))
      

      但是,第二种方法是使用 rowwise 的正确方法.

      Your second approach however, is the correct way to use rowwise.

      这篇关于了解rowwise()和c_across()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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