Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending in Column Name to ddply from Function

Tags:

r

plyr

I'd like to be able to send in a column name to a call that I am making to ddply. An example ddply call:

ddply(myData, .(MyGrouping), summarise, count=sum(myColumnName))

If I have ddply wrapped within another function is it possible to wrap this so that I can pass in an arbitrary value as myColumnName to the calling function?

like image 760
Dave Avatar asked Apr 16 '12 16:04

Dave


2 Answers

There has got to be a better way. And I couldn't figure out how to make it work with summarise.

my.fun <- function(df, count.column) { 
  ddply(df, .(x), function(d) sum(d[[count.column]]))
}

dat <- data.frame(x=letters[1:2], y=1:10)

> my.fun(dat, 'y')
  x V1
1 a 25
2 b 30
> 
like image 132
Justin Avatar answered Nov 09 '22 15:11

Justin


As what @David Arenburg said, this question is pretty old. Today, either data.table or dplyr package can give you the same result with a much faster speed.

Here is the data.table version of the answer.

library(data.table)
my.fun <- function(myData, MyGrouping, myColumnName) { 
  setDT(myData)[, lapply(.SD, sum), by=MyGrouping, .SDcols=myColumnName]
}
like image 5
Hao Avatar answered Nov 09 '22 14:11

Hao