Get and process the whole line in ddply in function

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')) }
#this doesn't work!
ddply(df,.(a,b,c), transform, foo=f(row))

, , , - ddply args , , .

: , , , ,

+3
2

ddply, . , ddply , (, ddply(df, names(df), f).

? Apply .

apply(df, 1, function(x) f(as.data.frame(t(x)))))

:

[1]  6  6  6  6  6 11 11 11 11 11 16 16 16 16 16 21 21 21 21 21
+4

...

df$id = 1:nrow(df)
ddply(df,c('id'),function(x){ ... })

adply(df,1,function(x){ ... })
0

All Articles