It is easy to capture one or more of ddply for processing, but is there a way to capture the entire current line and pass it to the function? Or capture a set of columns defined at runtime?
Let me illustrate:
For data, for example
df = data.frame(a=seq(1,20), b=seq(1,5), c= seq(5,1))
df
a b c
1 1 1 5
2 2 2 4
3 3 3 3
I could write a function to sum the named columns along the row of the data frame as follows:
selectiveSummer = function(row,colsToSum) {
return(sum(row[,colsToSum]))
}
It works when I call it for a string as follows:
> selectiveSummer(df[1,],c('a','c'))
[1] 6
So I would like to wrap this in an anonymous function and use it in ddply to apply it to each row of the table, something like the example below
f = function(x) { selectiveSummer(x,c('a','c')) }
ddply(df,.(a,b,c), transform, foo=f(row))
, , , - ddply args , , .
: , , , ,