我正在尝试学习将列表内的对象用作FUN中的lapply参数的不同方法。取得以下数据:

A <- list(a = matrix(0, ncol = 3, nrow = 3), b = matrix(0, ncol = 3, nrow = 3))
B <- list(a = matrix(1, ncol = 1, nrow = 3), b = matrix(1, ncol = 1, nrow = 3))
D <- mapply(FUN="list", A, B, SIMPLIFY=F, USE.NAMES=F)
D <- lapply(D, `names<-`, c("first", "second"))
D
[[1]]
[[1]]$`first`
     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    0    0
[3,]    0    0    0

[[1]]$second
     [,1]
[1,]    1
[2,]    1
[3,]    1


[[2]]
[[2]]$`first`
     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    0    0
[3,]    0    0    0

[[2]]$second
     [,1]
[1,]    1
[2,]    1
[3,]    1

所需结果:
[[1]]
     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    0    0
[3,]    0    0    0
[4,]    1    1    1

[[2]]
     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    0    0
[3,]    0    0    0
[4,]    1    1    1

这就是我通常会这样做的方式:
lapply(D, function(x) rbind(x$first, as.numeric(x$second)))

现在,我想知道是否有一种方法可以避免使用function(x)并重复所有这些xs。就像是:
lapply(D, "rbind", <args>)

如何让rbind(或任何其他函数)知道我正在引用lapply框架内的对象?

谢谢,

K.

最佳答案

为了“避免使用function(x)并重复所有这些x s”,我们可以使用with():

lapply(D, with, rbind(first, as.numeric(second)))

10-08 00:31