0

如何将向量/列列表传递给 plyer:ddply 内联函数?此代码有效:

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) 
                     ) 
               )

但我想做这样的事情(抛出错误,警告):

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)

谢谢你的建议。

4

1 回答 1

4

你想要sapply而不是lapply

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

lapply将返回 a list,正如错误所说,它不是原子的,而sapply将尝试简化结果 - 在您的情况下为数字向量,即您第一次尝试返回的类型。

但对于您的示例来说,更好的是colwise

ddply(olddf, .(V1, V2), colwise(mean))
于 2014-05-31T17:59:08.083 回答