0

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.

Jaap
  • 81,064
  • 34
  • 182
  • 193
RayR
  • 57
  • 1
  • 9
  • Without looking further into what this function does - what about `colnames(ag.gg)<-c(xcat, yvar, group)`? – lukeA Apr 22 '14 at 21:34
  • lukeA, you solved my temporary problem thank you. I am new to stackoverflow. How do I give you credit? – RayR Apr 22 '14 at 21:54
  • Then welcome to SO. :) There's a check mark next to the votes counter. If you find a solution that you like, you can accept that as an answer by clicking on the check mark. PS: Read http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example to provide good examples and get more answers. – lukeA Apr 22 '14 at 23:01

3 Answers3

1

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
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
0

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.

rsoren
  • 4,036
  • 3
  • 26
  • 37
  • Unfortunately the parse solution did not work. Thanks though. I downloaded the pdf from the fortunes package. Thanks all – RayR Apr 22 '14 at 22:10
0

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

lukeA
  • 53,097
  • 5
  • 97
  • 100