传递数据帧在功能内变异 [英] passing data frame to mutate within function

查看:132
本文介绍了传递数据帧在功能内变异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想传递一个数据框及其列,以便在函数内由dplyr的mutate进行处理。



这是一个例子

  multifun<  -  function ,变量){
mutate(dataf,newvar = vari * 2)
}

multifun(mtcars,gear)
pre>

此功能的问题是变量'gear'不是可识别的对象。更具体地说,我得到错误

  mutate_impl(.data,named_dots(...),environment())中的错误
对象'齿轮'未找到

这是dplyr的mutate正在寻找的环境的问题



我明白

  multifun(mtcars ,mtcars $ gear)

将给我我想要的答案,即

  mpg cyl disp hp drat wt qsec vs am gear carb newvar 
1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 8
2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 8
3 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 8

但是我想看看是否有一种避免在函数调用中引用数据框中使用的每个变量的方法。


$ b $我也知道,从突变出来e函数调用工作没有问题。即, mutate(mtcars,newvar = gear * 2)做这个工作。但是,我想了解在放置在函数中时,dplyr的变体在不同的环境中寻找变量。



提前非常感谢。
robert

解决方案

看看@Anandas解决方案,这似乎是最简单的hack

  multifun<  -  function(dataf,vari){
dataf< - mutate(dataf,newvar = dataf [,vari] * 2)
return(dataf)
}

multifun(mtcars,gear)

再次,使用@Anandas建议,您也可以执行

  multifun<  -  function dataf,vari){
vari< - deparse(substitute(vari))
dataf< - mutate(dataf,newvar = dataf [,vari] * 2)
return(dataf)
}

multifun(mtcars,gear)


I would like to pass a data frame and its columns to be processed by dplyr's mutate within a function.

Here is an example

multifun <- function(dataf,vari){
 mutate(dataf,newvar=vari*2)
}

multifun(mtcars,gear)

The problem with this function is that the variable 'gear' is not a recognized object. More specifically I get the error

Error in mutate_impl(.data, named_dots(...), environment())
object 'gear' not found 

This is a problem with the environment where dplyr's mutate is looking for the variable in question.

I understand that

multifun(mtcars,mtcars$gear)

will give me the answer that I want, namely

    mpg  cyl  disp  hp   drat  wt   qsec  vs am   gear carb newvar
1  21.0   6   160.0 110  3.90 2.620 16.46  0  1    4    4      8
2  21.0   6   160.0 110  3.90 2.875 17.02  0  1    4    4      8
3  22.8   4   108.0  93  3.85 2.320 18.61  1  1    4    1      8

but I would like to see if there is a way of avoiding the need to reference each variable used from the data frame in the function call.

I am also aware that taking mutate out of the function call works without problems. Namely, mutate(mtcars,newvar=gear*2) does the job. However, I am trying to understand how dplyr's mutate is looking for the variable in question in the different environments when placed inside a function.

Many thanks in advance. robert

解决方案

Looking at @Anandas solution, this seems to be simplest hack

multifun <- function(dataf, vari){   
dataf <- mutate(dataf, newvar = dataf[, vari]*2);   
return(dataf) 
}

multifun(mtcars,"gear")

Again, taking incount @Anandas suggestion, you could also do

multifun <- function(dataf, vari){  
  vari <- deparse(substitute(vari))
  dataf <- mutate(dataf, newvar = dataf[, vari]*2)   
  return(dataf) 
}

multifun(mtcars, gear)

这篇关于传递数据帧在功能内变异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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