Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R/Splus中将两列转换为一列的更好方法?_R_Matrix - Fatal编程技术网

在R/Splus中将两列转换为一列的更好方法?

在R/Splus中将两列转换为一列的更好方法?,r,matrix,R,Matrix,目前我做了以下工作: x <- cbind(c(1, 2, 3), c(4, 5, 6)) x.merged <- matrix(t(x), ncol=1) 编辑2 矩阵中的可选参数之一是按行填充。我们可以在这里使用它,它与您的原始解决方案非常相似。如果更符合您的喜好,您显然可以将ncol换成nrow matrix(x, ncol = 1, byrow = TRUE) [,1] [1,] 1 [2,] 2 [3,] 3 [4,] 4 [5,]

目前我做了以下工作:

x <- cbind(c(1, 2, 3), c(4, 5, 6))
x.merged <- matrix(t(x), ncol=1)

编辑2

矩阵
中的可选参数之一是按行填充。我们可以在这里使用它,它与您的原始解决方案非常相似。如果更符合您的喜好,您显然可以将
ncol
换成
nrow

matrix(x, ncol = 1, byrow = TRUE)
     [,1]
[1,]    1
[2,]    2
[3,]    3
[4,]    4
[5,]    5
[6,]    6
我发誓昨天就有这样的问题,但我找不到。让我们看看我是否能记住这个问题的一些答案:

#do.call will execute a function, "c" for combine in this case, over a list so we coerce the 
#matrix to a list
do.call("c", as.list(x))
[1] 1 2 3 4 5 6


#Similar concept with stack, but it expects a data.frame 
stack(as.data.frame(x))
  values ind
1      1  V1
2      2  V1
3      3  V1
4      4  V2
5      5  V2
6      6  V2

#The melt function in package reshape can do this and a lot more when combined with "cast"
library(reshape)
melt(x)
  X1 X2 value
1  1  1     1
2  2  1     2
3  3  1     3
4  1  2     4
5  2  2     5
6  3  2     6

编辑:似乎我应该能够找到问题,因为我提供了答案,但我离题了:

如果你有一个矩阵而不是一个数据帧,没有理由不使用
t
。请注意,
stack
使用
as.list
lappy
,因此在这种情况下可能不那么优雅。然而,如果你想要的只是一个向量,你可以省略
矩阵(*,ncol=1)
位;只需将
用作.vector

实际上,如果将其设为向量,所需的时间不超过:

as.vector(matrix(c(1:3,4:6),byrow=T,nrow=2))
[1] 1 4 2 5 3 6
> c(t(x))
[1] 1 4 2 5 3 6
或者,如果您确实必须避免
t()
,那么您可以:

> c(apply(x,2,rbind))
[1] 1 4 2 5 3 6
这适用于任意数量的列,但包括应用。如果不想使用该列,则必须手动指定要粘贴在后面的所有列。但t()解决方案是目前最快的:

> n <- 10000000    
> x <- matrix(rnorm(n),ncol=2)
> system.time(c(t(x)))
   user  system elapsed 
   0.07    0.00    0.06 

> system.time(c(rbind(x[,1],x[,2])))
   user  system elapsed 
   0.22    0.05    0.26 
只要记住矩阵是按列读取的,就行了。所以在你的情况下,你需要

> t(x)[4]
[1] 5
如果您确实需要它作为矩阵,那么:

> matrix(t(x))
     [,1]
[1,]    1
[2,]    4
[3,]    2
[4,]    5
[5,]    3
[6,]    6

我认为用最少的击键来实现这一点的方法是:

c(rbind(c(1, 2, 3), c(4, 5, 6)))

使用
rbind
可以消除转置,而
c
是从对象中剥离属性(包括维度)的最短方法,尽管这样使用有点滥用。

-1因为没有回答问题-即如何交错值。@Joris:不确定
byrow=TRUE
为什么会得到+1。忽略它或显式设置
byrow=FALSE
将产生完全相同的结果。我第一次尝试使用
byrow=TRUE
来解决这个问题,很惊讶它没有按照我想要的方式交错结果。我更惊讶的是,
byrow
选项似乎对结果没有影响。@brianjd:byrow在只有一列时没有影响。按行填充,每行使用一个值,与按列填充是一样的。@Tyler:对,这是最清楚的表达方式。我最初的困惑是认为
matrix()
(带有选项
byrow=TRUE
set)的输入是按行接收的,而不是按行写入的输出。@brianjd:你说得对。我读了我先前的回答。你完美地描述了我的困惑。我太快了。刚刚编辑了上面的问题。既然
x
是给我的,我就得把你的解改成
c(rbind(x[,1],x[,2])
@brianjd:那你最好用t(),另请看我的答案。我编辑了上面的问题。因为
x
是给我的,所以我必须把你的解改为:
as.vector(矩阵(c(x),byrow=T,nrow=2))
。但是
c(t(x))
也会这样做。我正在使用
c(t(x))
。甚至不知道
c()
会以这种方式剥离维度!谢谢你精彩的回答。
> matrix(t(x))
     [,1]
[1,]    1
[2,]    4
[3,]    2
[4,]    5
[5,]    3
[6,]    6
c(rbind(c(1, 2, 3), c(4, 5, 6)))