1

我有一个值矩阵:

x<-matrix(rnorm(8),nrow=2,ncol=4,byrow=T)
           [,1]       [,2]       [,3]      [,4]
[1,] -0.1048800  0.4437521 -0.7768075 0.2820776
[2,]  0.0700801 -0.4621662 -0.7877975 0.4933406

我想让它看起来像这样:

           [,1]      
[1,] -0.1048800
[2,]  0.0700801
[3,]  0.4437521
[4,] -0.4621662
[5,] -0.7768075
[6,] -0.7877975
[7,]  0.2820776
[8,]  0.4933406

我试过了:

  temp<-c()
  for(l in 1:ncol(x)){
    temp<-rbind(temp,as.x[,l])
  }

但它不会让它工作。有任何想法吗?

4

4 回答 4

4

为什么不只是matrix(x,8,1)

x<-matrix(rnorm(8),nrow=2,ncol=4,byrow=T)
> matrix(x,8,1)
           [,1]
[1,] -1.2735095
[2,] -0.8340542
[3,] -1.0982551
[4,]  0.8774815
[5,]  1.0443129
[6,] -0.1672568
[7,] -0.3545977
[8,] -1.2148138

或者正如 thelatemail 所指出的,更一般的matric(x,ncol = 1).

于 2013-05-23T21:52:43.773 回答
4

我认为这样做:

x<-matrix(rnorm(8),nrow=2,ncol=4,byrow=T)
temp <- as.matrix(as.vector(x))
于 2013-05-23T21:54:05.500 回答
3

记住矩阵是一个带有dim属性的向量......

您可以dim直接更改属性以执行...

dim(x) <- c(ncol(x)*nrow(x),1)
于 2013-05-23T21:56:48.857 回答
2

一种奇特的方式是使用attr如下:

> set.seed(1)
> x<-matrix(rnorm(8),nrow=2,ncol=4,byrow=T)
> attr(x, "dim") <- c(prod(dim(x)), 1)
> x
           [,1]
[1,] -0.6264538
[2,]  0.3295078
[3,]  0.1836433
[4,] -0.8204684
[5,] -0.8356286
[6,]  0.4874291
[7,]  1.5952808
[8,]  0.7383247
于 2013-05-23T22:09:34.597 回答