本文介绍了如何将矩阵转换为 R 中的列向量列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设您要将矩阵转换为列表,其中列表的每个元素都包含一列.list()
或 as.list()
显然不起作用,直到现在我使用 tapply
的行为进行黑客攻击:
Say you want to convert a matrix to a list, where each element of the list contains one column. list()
or as.list()
obviously won't work, and until now I use a hack using the behaviour of tapply
:
x <- matrix(1:10,ncol=2)
tapply(x,rep(1:ncol(x),each=nrow(x)),function(i)i)
我对此并不完全满意.有人知道我忽略的更清洁的方法吗?
I'm not completely happy with this. Anybody knows a cleaner method I'm overlooking?
(为了制作一个充满行的列表,代码显然可以改为:
(for making a list filled with the rows, the code can obviously be changed to :
tapply(x,rep(1:nrow(x),ncol(x)),function(i)i)
)
推荐答案
为了给猫剥皮,把数组当作一个向量,就好像它没有dim属性一样:
In the interests of skinning the cat, treat the array as a vector as if it had no dim attribute:
split(x, rep(1:ncol(x), each = nrow(x)))
这篇关于如何将矩阵转换为 R 中的列向量列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!