0

How can I pass a vector/list of columns to a plyer:ddply inline function? This code works:

newdf <-ddply(olddf, .(V1, V2), function(df)
                    c( mean(df$V3), +
                       mean(df$V4), +
                       mean(df$V5), +
                       mean(df$V6), +
                       mean(df$V7), +
                       mean(df$V8), +
                       mean(df$V9), +
                       mean(df$V10), +
                       mean(df$V11), +
                       mean(df$V12), +
                       mean(df$V13), +
                       mean(df$V14), +
                       mean(df$V15), +
                       mean(df$V16), +
                       mean(df$V17), +
                       mean(df$V18), +
                       mean(df$V19), +
                       mean(df$V20) 
                     ) 
               )

But I'd like to do something like this (which throws error, warnings):

newdf <-ddply( olddf, .(V1, V2), function(df)  lapply(df[,3:20], mean) )

Error in list_to_dataframe(res, attr(.data, "split_labels"), .id, id_as_factor) : 
  Results must be all atomic, or all data frames
In addition: There were 50 or more warnings (use warnings() to see the first 50)

Thanks for advice.

Jaap
  • 81,064
  • 34
  • 182
  • 193
John Williams
  • 75
  • 1
  • 1
  • 8

1 Answers1

4

You want sapply and not lapply:

ddply(olddf, .(V1, V2), function(df) sapply(df[,3:20], mean) )

lapply will return a list, which is not, as the error says, atomic, while sapply will attempt to simplify the results--in your case into a numeric vector, the type returned by your first attempt.

But even better for your example is colwise:

ddply(olddf, .(V1, V2), colwise(mean))
Peyton
  • 7,266
  • 2
  • 29
  • 29