5

Is there any way to assign a variable's value to the resultant column name in plyr?

So in this code:

column_name <- 'total'
df <- data.frame(a=c('a','b'), b=c(1,2))
ddply(df, .(a), summarise, column_name=sum(b))

As you know, this spits out a data frame which consists of variables a and column_name. However, what I want to get it is variables a and total, where total is assigned dynamically by a variable's value, since actually, I want to process it inside a loop, that I can't specify it directly inside ddply function.

For you information, this code doesn't work.

ddply(df, .(a), summarise, get(column_name)=sum(b))

Is there any solution to deal with it?

starball
  • 20,030
  • 7
  • 43
  • 238
Blaszard
  • 30,954
  • 51
  • 153
  • 233
  • http://stackoverflow.com/questions/14721592/r-dynamically-build-list-in-data-table-or-ddply may be useful to point you in a direction. – mnel May 09 '13 at 06:06

1 Answers1

6

There are several ways:

> column_names <- c('total', 'latot')
> df <- data.frame(a=c('a','b'), b=c(1,2))
> 
> # temporal variable
> for (cn in column_names) {
+   ret <- ddply(df, .(a), summarise, ..x=sum(b))
+   ret <- rename(ret, c(..x = cn))
+   print(ret)
+ }
  a total
1 a     1
2 b     2
  a latot
1 a     1
2 b     2
> 
> # ept solution
> for (cn in column_names) {
+   print(eval(parse(text = paste0("ret <- ddply(df, .(a), summarise,", cn, "=sum(b))"))))
+ }
  a total
1 a     1
2 b     2
  a latot
1 a     1
2 b     2
> 
> # dynamic generation of call
> for (cn in column_names) {
+   args <- alist(df, .(a), summarize, sum(b))
+   names(args) <- c("", "", "", cn)
+   print(do.call("ddply", args))   
+ }
  a total
1 a     1
2 b     2
  a latot
1 a     1
2 b     2
kohske
  • 65,572
  • 8
  • 165
  • 155
  • Actually, I first went with eval and replaced it later with rename function, as I have a pathological hatred for calling either eval or do.call. Just wanted to know if it's feasible in one line. Thanks for the response though. – Blaszard May 09 '13 at 14:05