如何将用户定义函数中作为参数的变量传递给R中的子函数 [英] How to pass variables that are arguments in user defined functions to subfunctions in r

查看:215
本文介绍了如何将用户定义函数中作为参数的变量传递给R中的子函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解如何创建一个用户定义的函数时遇到了一个普遍的问题,该函数可以接受变量作为可以在定义的函数内部进行操作的参数。我想创建一个函数,在其中我可以将变量作为参数传递给内部函数进行操作。似乎我要使用的许多函数都需要 c()运算符,该运算符需要在参数周围加上引号。

I have a general problem in understanding how to create a user defined function that can accept variables as arguments that can be manipulated inside the defined function. I want to create a function in which I can pass variables as arguments to internal functions for manipulation. It appears that many of the functions I want to use require the c() operator which requires quotes around the arguments.

因此,我的函数必须能够将数据帧中变量的名称传递给 c()的引号以及其他需要使用引号字符串的函数。我读了很多关于 paste0 paste cat(x),但我无法弄清楚如何完全解决我的问题。

So my function has to be able to pass the name of a variable from a dataframe into the quotes for c() and other functions requiring quote strings. I read through many post on paste0, paste and cat(x), but I cannot figure out how to solve my problem completely.

这是一个简单的数据集,并缩短了代码以帮助解决问题。在这里,我只希望能够提供一个数据框和三个变量。该函数应为 x z 变量的每个组合提供y位置变量的平均值。结果汇总表应具有作为XTABAR参数提供的变量名称作为列标题。

Here is a simple dataset and shortened code to help structure the problem. Here I just want to be able to provide a dataframe, and three variables. The function should provide the mean of the variable in the y position for each combo of the x and z variable. The resultant aggregate table should have the names of the variables provided as arguments to XTABAR as column headers.

n=50
DataTest = data.frame(  xcol=sample(1:3, n, replace=TRUE), ycol = rnorm(n, 5, 2), Catg=letters[1:5])

XTABAR<- function(DS,xcat,yvar,group){ 
  library(plyr)
  #library(ggplot2)
  #library(dplyr)
  #library(scales)
  localenv<-environment()
  gg<-data.frame(DS,x=DS[,xcat],y=DS[,yvar],z=DS[,group] )
  cnames<-colnames(gg)
  ag.gg<-aggregate(gg$y, by=list(gg$x,gg$z),FUN=mean)

  colnames(ag.gg)<-c(cat('"',cnames[1],'"'),cat('"',cnames[2],'"'),cat('"',cnames[3],'"'))
  return(ag.gg)
}

XTABAR(DataTest,"xcol","ycol","Catg")

这段代码与我解决简单问题的方式非常接近。我不知道如何从列名中删除引号,也不知道如何摆脱NA。

This code is as close as I can get to solving the simple problem. I don't know how to remove the quotes from the column names nor how to get rid of the NA's.

谢谢您对逻辑和或代码的任何帮助。

Thank you for any help on the logic and or code.

推荐答案

尝试以下操作。我不太希望引用名称,但在下面的代码中将星号括起来。如果不需要,请删除 setNames 语句。

Try the following. I was not too clear about the desire to quote the names but we put stars around them in the code below. If that is not needed then remove the setNames statement.

XTABAR <- function(DS, xcat, yvar, group) {
    ag <- aggregate(DS[yvar], DS[c(xcat, group)], mean)
    setNames(ag, paste0("*", names(ag), "*"))
}

测试一下:

XTABAR(DataTest, "xcol", "ycol", "Catg")

给予:

   *xcol* *Catg*   *ycol*
1       1      a 5.700938
2       2      a 5.292628
3       3      a 5.204395
4       1      b 4.054289
5       2      b 5.119659
6       3      b 4.050799
7       1      c 2.937309
8       2      c 5.696256
9       3      c 6.773029
10      1      d 5.323572
11      2      d 3.430644
12      3      d 4.892041
13      1      e 4.024070
14      3      e 5.038122

这篇关于如何将用户定义函数中作为参数的变量传递给R中的子函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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