且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将 n 个函数的列表应用于数据帧的每一行?

更新时间:2023-08-28 16:41:34

忽略你的代码片段并坚持你想要在列号 jj 的初始规范/em> 然后总结结果"...你可以这样做:

Ignoring your code snippet and sticking to your initial specification that you want to apply function j on the column number j and then "sum the results"... you can do:

mapply( do.call, funs, lapply( mydata, list))
#      [,1] [,2]      [,3] [,4]
# [1,]    1   27 0.8414710    2
# [2,]    4    8 0.9092974    3
# [3,]    9    1 0.9092974    3

我不确定您现在想要以哪种方式添加结果(即按行或按列),因此您可以执行 rowSumscolSums在这个矩阵上.例如:

I wasn't sure which way you want to now add the results (i.e. row-wise or column-wise), so you could either do rowSums or colSums on this matrix. E.g:

colSums( mapply( do.call, funs,  lapply( mydata, list)) )
# [1] 14.000000 36.000000  2.660066  8.000000