【发布时间】:2014-06-11 05:25:59
【问题描述】:
我想将整数矩阵列表转换为数字。我知道 lapply 对内部结构不友好,但是有 lapply 的解决方案吗?
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)
【问题讨论】:
-
+1。我以前从未想过这种行为。
-
我也是。我刚刚通过阅读这个问题学到了很多东西。有趣的东西。
-
+.5 表示好问题 +.5 表示好标题 :)
-
嗯,标题被证明是错误的......是索引,而不是 lapply,刺穿......