[R] simple coversion of matrix rows to matrix columns
arun
smartpink111 at yahoo.com
Wed Nov 7 13:48:06 CET 2012
Hi,
is.matrix(a)
#[1] TRUE
#You don't need t(matrix(a))
t(a)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#[1,] 1 2 3 4 5 6 7 8 9 10
#[2,] 11 12 13 14 15 16 17 18 19 20
#[3,] 21 22 23 24 25 26 27 28 29 30
is.matrix(t(a))
#[1] TRUE
str(t(a))
# int [1:3, 1:10] 1 11 21 2 12 22 3 13 23 4 ...
str(t(matrix(a)) )
# int [1, 1:30] 1 2 3 4 5 6 7 8 9 10 ...
A.K.
----- Original Message -----
From: Andras Farkas <motyocska at yahoo.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Wednesday, November 7, 2012 6:50 AM
Subject: [R] simple coversion of matrix rows to matrix columns
Dear All
I would like to convert matrix rows to columns. I am thinking the t() function should help, but am having a hard time converting the matrix into the dimensions I would like them to. Example:
a <-matrix(c(1:30),ncol=3) gives me:[,1] [,2] [,3]
[1,] 1 11 21
[2,] 2 12 22
[3,] 3 13 23
[4,] 4 14 24
[5,] 5 15 25
[6,] 6 16 26
[7,] 7 17 27
[8,] 8 18 28
[9,] 9 19 29
[10,] 10 20 30
I would like to convert the above into a matrix that looks like this with 3 rows and 10 columns:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
I have tried :
b <-t(matrix(a))
but I am getting a 1X30 double matrix. I also Tried to specify
b <-t(matrix(a),byrow=TRUE,ncol=10)
but that does not help either...
apreciate your help,
Andras
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list