gpt4 book ai didi

r - 如何从 lapply-attack 中保存我的矩阵列表的结构?

转载 作者:行者123 更新时间:2023-12-04 13:07:09 25 4
gpt4 key购买 nike

我想将整数矩阵列表转换为数字。我知道lapply对内部结构不友好,但是有没有lapply-solution?

mtList = list(matrix(sample(1:10),nrow=5),
matrix(sample(1:21),nrow=7))
str(mtList)

# This works, and I could wrap it in a for loop
mtList[[1]][] = as.numeric(mtList[[1]])
mtList[[2]][] = as.numeric(mtList[[2]])
str(mtList)

# But how to use lapply here? Note that the internal
# matrix structure is flattened
mtList1 = lapply(mtList,function(x)x[] = as.numeric(x))
str(mtList1)

最佳答案

您必须在您的 lapply 主力函数中返回值:

mtList1 = lapply(mtList,function(x) {
x[] = as.numeric(x)
x
})

str(mtList1)
# List of 2
# $ : num [1:5, 1:2] 1 7 6 3 9 10 5 2 8 4
# $ : num [1:7, 1:3] 21 3 15 14 6 4 18 17 9 8 ...

关于r - 如何从 lapply-attack 中保存我的矩阵列表的结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23294108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com