R 组合列表子集中的向量
我想通过组合列表子集中的向量得到一个矩阵R 组合列表子集中的向量,r,list,R,List,我想通过组合列表子集中的向量得到一个矩阵mat。按照使用for循环执行相同操作的方法。我想知道是否有更快的方法 i <- 1 # the subset mat<- matrix(NA, ncol = p, nrow = n) for (j in 1 : p) { mat[, j] <- list_of_list[[j]][[i]]$the_vector } 您只需取消列表列表,然后将其重塑为矩阵: matrix(unlist(list(list(1,2
mat
。按照使用for
循环执行相同操作的方法。我想知道是否有更快的方法
i <- 1 # the subset
mat<- matrix(NA, ncol = p, nrow = n)
for (j in 1 : p) {
mat[, j] <- list_of_list[[j]][[i]]$the_vector
}
您只需
取消列表
列表,然后将其重塑为矩阵
:
matrix(unlist(list(list(1,2,3,4),list(5,6,7,8),list(9,10,11,12))), nrow=3, byrow = T)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
您只需
取消列表
列表,然后将其重塑为矩阵
:
matrix(unlist(list(list(1,2,3,4),list(5,6,7,8),list(9,10,11,12))), nrow=3, byrow = T)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
您可以尝试
sapply()
功能:
i <- 1L
mat <- sapply(list_of_list, function(.x) .x[[i]]$the_vector)
mat
我还没有对代码进行基准测试,以确保它在执行速度方面更快,但它确实需要更少的按键
sapply()
在列表或向量上应用函数,是一种隐含的for
循环 您可以尝试sapply()
功能:
i <- 1L
mat <- sapply(list_of_list, function(.x) .x[[i]]$the_vector)
mat
我还没有对代码进行基准测试,以确保它在执行速度方面更快,但它确实需要更少的按键
sapply()
在列表或向量上应用函数,是一种隐含的for
循环 我不确定您是否正在寻找这样的产品。它将为您提供一个由3个矩阵组成的列表,这些矩阵对应于列表的子列表中的vector
mapply(list_of_list[[1]],list_of_list[[2]],
FUN = function(x,y){t(mapply(x$the_vector,y$the_vector,
FUN = function(u,v){matrix(c(u,v),ncol=2,byrow = F,dimnames = NULL)},
SIMPLIFY = T))},SIMPLIFY = F)
#[[1]]
# [,1] [,2]
#[1,] 1 0
#[2,] 2 0
#[3,] 3 0
#[4,] 4 0
#[5,] 5 0
#[[2]]
# [,1] [,2]
#[1,] 2 1
#[2,] 3 2
#[3,] 4 3
#[4,] 5 4
#[5,] 6 5
#[[3]]
# [,1] [,2]
#[1,] 3 22
#[2,] 4 44
#[3,] 5 66
#[4,] 6 88
#[5,] 7 110
我不确定你是否在找这样的东西。它将为您提供一个由3个矩阵组成的列表,这些矩阵对应于列表的子列表中的
vector
mapply(list_of_list[[1]],list_of_list[[2]],
FUN = function(x,y){t(mapply(x$the_vector,y$the_vector,
FUN = function(u,v){matrix(c(u,v),ncol=2,byrow = F,dimnames = NULL)},
SIMPLIFY = T))},SIMPLIFY = F)
#[[1]]
# [,1] [,2]
#[1,] 1 0
#[2,] 2 0
#[3,] 3 0
#[4,] 4 0
#[5,] 5 0
#[[2]]
# [,1] [,2]
#[1,] 2 1
#[2,] 3 2
#[3,] 4 3
#[4,] 5 4
#[5,] 6 5
#[[3]]
# [,1] [,2]
#[1,] 3 22
#[2,] 4 44
#[3,] 5 66
#[4,] 6 88
#[5,] 7 110
下面是另一个与@TUSHAr非常相似的解决方案,但可能更模块化:
## Lapply wrapping function that outputs a matrix
lapply.wrapper <- function(i, list_of_list) {
matrix(unlist(lapply(list_of_list, function(X, i) X[[i]]$the_vector, i = i)), ncol = length(list_of_list))
}
## Using the wrapper on the first subset:
lapply.wrapper(1, list_of_list)
# [,1] [,2]
#[1,] 1 0
#[2,] 2 0
#[3,] 3 0
#[4,] 4 0
#[5,] 5 0
## Applying the function to all subsets
sapply(1:length(list_of_list[[1]]), lapply.wrapper, list_of_list, simplify = FALSE)
#[[1]]
# [,1] [,2]
#[1,] 1 0
#[2,] 2 0
#[3,] 3 0
#[4,] 4 0
#[5,] 5 0
#
#[[2]]
# [,1] [,2]
#[1,] 2 1
#[2,] 3 2
#[3,] 4 3
#[4,] 5 4
#[5,] 6 5
#
#[[3]]
# [,1] [,2]
#[1,] 3 22
#[2,] 4 44
#[3,] 5 66
#[4,] 6 88
#[5,] 7 110
输出矩阵的lappy包装函数
lappy.wrapper这里有另一个与@TUSHAr非常相似的解决方案,但可能更模块化:
## Lapply wrapping function that outputs a matrix
lapply.wrapper <- function(i, list_of_list) {
matrix(unlist(lapply(list_of_list, function(X, i) X[[i]]$the_vector, i = i)), ncol = length(list_of_list))
}
## Using the wrapper on the first subset:
lapply.wrapper(1, list_of_list)
# [,1] [,2]
#[1,] 1 0
#[2,] 2 0
#[3,] 3 0
#[4,] 4 0
#[5,] 5 0
## Applying the function to all subsets
sapply(1:length(list_of_list[[1]]), lapply.wrapper, list_of_list, simplify = FALSE)
#[[1]]
# [,1] [,2]
#[1,] 1 0
#[2,] 2 0
#[3,] 3 0
#[4,] 4 0
#[5,] 5 0
#
#[[2]]
# [,1] [,2]
#[1,] 2 1
#[2,] 3 2
#[3,] 4 3
#[4,] 5 4
#[5,] 6 5
#
#[[3]]
# [,1] [,2]
#[1,] 3 22
#[2,] 4 44
#[3,] 5 66
#[4,] 6 88
#[5,] 7 110
输出矩阵的lappy包装函数
lapply.wrapper什么是列表的列表和向量?你能给出一个可复制的例子吗?完成了,希望例子清楚。那么你总共想要3个矩阵吗?什么是列表的列表和向量?你能给出一个可复制的例子吗?完成了,希望这个例子很清楚。那么你总共想要3个矩阵吗?我已经澄清了我的问题。这不是我想要的。谢谢。我已经把我的问题澄清了一点。这不是我想要的。谢谢,看起来不错。谢谢我想很难比这个做得更好。看起来不错。谢谢我想很难做得比这个更好。我想要的是@Uwe做的事情。谢谢。我想要的是@Uwe做的事情。谢谢