用原型包裹
在创建列表结构时,尝试将数据框包装在proto对象中:
library(proto)
L <- list(a = proto(DF = BOD), b = proto(DF = BOD))
rapply(L, f = function(.) colSums(.$DF), how = "replace")
Run Code Online (Sandbox Code Playgroud)
赠送:
$a
Time demand
22 89
$b
Time demand
22 89
Run Code Online (Sandbox Code Playgroud)
如果你想进一步rapply将你的函数的结果包装在一个proto对象中;
f <- function(.) proto(result = colSums(.$DF))
out <- rapply(L, f = f, how = "replace")
str(out)
Run Code Online (Sandbox Code Playgroud)
赠送:
List of 2
$ a:proto object
.. $ result: Named num [1:2] 22 89
.. ..- attr(*, "names")= chr [1:2] "Time" "demand"
$ b:proto object
.. $ result: Named num [1:2] 22 89
.. ..- attr(*, "names")= chr [1:2] "Time" "demand"
Run Code Online (Sandbox Code Playgroud)
2.编写自己的替代方案
recurse <- function (L, f) {
if (inherits(L, "data.frame")) f(L)
else lapply(L, recurse, f)
}
L <- list(a = BOD, b = BOD)
recurse(L, colSums)
Run Code Online (Sandbox Code Playgroud)
这给出了:
$a
Time demand
22 89
$b
Time demand
22 89
Run Code Online (Sandbox Code Playgroud)
增加:第二种方法