如何将矩阵转换为R中的列向量列表? [英] How to convert a matrix to a list of column-vectors in R?

查看:179
本文介绍了如何将矩阵转换为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中的列向量列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆