How to pass variables that are arguments in user d

2019-09-04 15:19发布

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.

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.

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

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.

3条回答
Evening l夕情丶
2楼-- · 2019-09-04 15:51

I make heavy use of eval(parse(text=)) for this purpose. It evaluates a character string as though it is a command. For example:

> x <- "5 + 5"
> eval(parse(text=x))
[1] 10

Using your example, this should work if you input your parameters as character strings:

XTABAR<- function(DS,xcat,yvar,group){ 
  library(plyr)
  #library(ggplot2)
  #library(dplyr)
  #library(scales)

  var1 <- eval(parse(text=paste(DS, "$", xcat, sep="")))
  var2 <- eval(parse(text=paste(DS, "$", yvar, sep="")))
  var3 <- eval(parse(text=paste(DS, "$", group, sep="")))

  localenv<-environment()
  gg<-data.frame(x=var1, y=var2, z=var3)
  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)

}

I'm going to go ahead and anticipate a criticism of my answer.

> require(fortunes)
Loading required package: fortunes
> fortune(106)

If the answer is parse() you should usually rethink
the question.
   -- Thomas Lumley
      R-help (February 2005)

Mr. Lumley is probably correct in this case. There are probably simpler solutions, but this should at least get you going.

查看更多
家丑人穷心不美
3楼-- · 2019-09-04 15:56

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), "*"))
}

Test it:

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

giving:

   *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
查看更多
做个烂人
4楼-- · 2019-09-04 16:07

To set the column names, use colnames(ag.gg) <- c(xcat, yvar, group).

查看更多
登录 后发表回答