[英]Putting rows as a single column in r
I have the following data.frame x 我有以下data.frame x
a d g
b e h
c f i
Note : letters are used as an example. 注意 :以字母为例。 The actual data.frame has numbers in place of letters (real data which are not in ascending order, as in this example)
实际的data.frame有数字代替字母(实际数据不按升序排列,如本例所示)
I want to transform it in an unique column, by putting columns from 2 to 4 under the first. 我希望通过在第一列中放置2到4列来将其转换为唯一列。 The expected result is the following
预期结果如下
a
b
c
d
e
f
g
h
i
I tried the following code 我尝试了以下代码
(t(x), ncol=1, nrow=ncol(x)*nrow(x), byrow=F)
but it gives (obviously) the following 但它(显然)给出了以下内容
a
d
g
b
e
h
c
f
i
Something like this? 像这样的东西?
X <- matrix(letters[1:9], ncol=3)
matrix(X, ncol=1)
R matrices are in column major order , so you can easily concatenate them into a single column vector with the matrix
function. R矩阵按列主要顺序排列 ,因此您可以使用
matrix
函数轻松将它们连接成单个列向量。
I find stack
also very useful 我发现
stack
也很有用
X <- data.frame(matrix(letters[1:9], ncol=3),stringsAsFactors=FALSE)
stack(X)[,"values",drop=FALSE]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.