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?
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
>
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]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With