[R] as.matrix and one row

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue Mar 28 18:40:06 CEST 2006


Federico Calboli wrote:

> Hi All,
> 
> I have the following problem:
> 
> x = c(1,2)
> x
> [1] 1 2
> 
> as.matrix(x)
>      [,1]
> [1,]    1
> [2,]    2
> 
> BUT, if I add:
> 
> y = c(3,4)
> 
> as.matrix(rbind(x,y))
>   [,1] [,2]
> x    1    2
> y    3    4
> 
> It does not transpose. Since I will need as.matrix() for a list of data
> that is in one or more lines, I need as.matrix to behave in a consisten
> fashions, so I get 
> 
> as.matrix(x, whatever)
>   [,1] [,2]
> x    1    2
> 
> and 
> 
> as.matrix(rbind(x,y), whatever)
>   [,1] [,2]
> x    1    2
> y    3    4
> 
> I tried byrow =T, does not make a thing.

as long as you do not tell R the dimensions. Hence
  matrix(x, byrow = TRUE, ncol = 2, .....)
should do the trick.

Uwe Ligges


> 
> Regards,
> 
> Federico Calboli
>




More information about the R-help mailing list