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)
)
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…