I tried with the following code
rbind(1, matrix(c(1,2,3,4,5,6,7,8,9,10), 5))
[,1] [,2]
[1,] 1 1
[2,] 1 6
[3,] 2 7
[4,] 3 8
[5,] 4 9
[6,] 5 10
but I wish to get output like below
[,1] [,2]
[1,] 1
[2,] 1 6
[3,] 2 7
[4,] 3 8
[5,] 4 9
[6,] 5 10
cbind a single vector with NA and then use rbind
rbind(cbind(1, NA),matrix(1:10, 5))
# [,1] [,2]
#[1,] 1 NA
#[2,] 1 6
#[3,] 2 7
#[4,] 3 8
#[5,] 4 9
#[6,] 5 10
For purposes of getting the exact output, we can do the following(see the note below):
noquote(rbind(c(1,""),matrix(c(1,2,3,4,5,6,7,8,9,10), 5)))
[,1] [,2]
[1,] 1
[2,] 1 6
[3,] 2 7
[4,] 3 8
[5,] 4 9
[6,] 5 10
NOTE
Using "" to introduce a blank will lead to coercion to character.
We could use as.numeric to have numerics but this would lead to NAs which has already been demonstrated.
Using NA instead of "" is more realistic and useful
Related
I have a list of matrices where most of the matrices are column matrices but some of them are row matrices. How to convert only those row matrices to column matrices? I would like to achieve this using base R.
Here is the list of matrices where the third one is a row matrix
x <- list(`1` = matrix(1:20, nrow=5), `2` = matrix(1:20, nrow=10), `3` = matrix(1:5, nrow=1))
How to convert the list to one like this:
$`1`
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
$`2`
[,1] [,2]
[1,] 1 6
[2,] 2 7
[3,] 3 8
[4,] 4 9
[5,] 5 10
$`3`
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
I have a much larger dataset and so efficient code is preferred!
Check the dimensions of the matrix and transpose it if the row dimension is 1:
(y <- lapply(x, function(x) if(dim(x)[1] == 1) { t(x)} else x))
# $`1`
# [,1] [,2] [,3] [,4]
# [1,] 1 6 11 16
# [2,] 2 7 12 17
# [3,] 3 8 13 18
# [4,] 4 9 14 19
# [5,] 5 10 15 20
#
# $`2`
# [,1] [,2]
# [1,] 1 11
# [2,] 2 12
# [3,] 3 13
# [4,] 4 14
# [5,] 5 15
# [6,] 6 16
# [7,] 7 17
# [8,] 8 18
# [9,] 9 19
# [10,] 10 20
#
# $`3`
# [,1]
# [1,] 1
# [2,] 2
# [3,] 3
# [4,] 4
# [5,] 5
I want to iterate the following matrix and print sets of 2 cell values. Is there a way to do this without a for-loop?
Input:
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 4 7 10 13 16
[2,] 2 5 8 11 14 17
[3,] 3 6 9 12 15 18
Expected Output:
[,1] [,2]
[1,] 1 4
[2,] 7 10
[3,] 13 16
[4,] 2 5
[5,] 8 11
[6,] 14 17
[7,] 3 6
[8,] 9 12
[9,] 15 18
This my code:
mat<-matrix(data=seq(1:18), nrow=3,ncol=6)
r <- rep(seq(1,3),each=2)
c1 <- seq(1,6,2)
c2 <- seq(2,6,2)
m <- mat[r,c(c1:c2)] # This does not work, it only output first two cells
We can get the transpose of the matrix, then convert back to matrix by specifying the ncol
matrix(t(mat), ncol=2, byrow=TRUE)
# [,1] [,2]
# [1,] 1 4
# [2,] 7 10
# [3,] 13 16
# [4,] 2 5
# [5,] 8 11
# [6,] 14 17
# [7,] 3 6
# [8,] 9 12
# [9,] 15 18
I don't understand why I cannot order a matrix based on a vector using the order function
I have the following:
m
[,1] [,2]
[1,] 1 5
[2,] 2 5
[3,] 3 5
[4,] 4 5
[5,] 5 5
[6,] 6 5
v
[[1]]
[1] 3 1 2 4 5 6
When I use:
m[order(unlist(v)),]
I get the following, incorrectly ordered matrix.
[,1] [,2]
[1,] 2 5
[2,] 3 5
[3,] 1 5
[4,] 4 5
[5,] 5 5
[6,] 6 5
when the order that I want is what's in v
[,1] [,2]
[1,] 3 5
[2,] 1 5
[3,] 2 5
[4,] 4 5
[5,] 5 5
[6,] 6 5
Why do you guys think this is happening and how can I fix it?
Instead of
m[order(unlist(v)),]
Try
temp <- unlist(v)
m[ temp , ]
Because order returns the indexes in the order that you desire. E.g.
> x = c(3,1,2)
> order(x)
[1] 2 3 1
> x[order(x)]
[1] 1 2 3
The function mapply() appears not to properly work in the following case:
a <- list(matrix(1:8,4,2),matrix(1:9,3,3))
b <- list(1:4,1:3)
mapply(a,b,FUN=cbind)
that gives the following matrix
[,1] [,2]
[1,] 1 1
[2,] 2 2
[3,] 3 3
[4,] 4 4
[5,] 5 5
[6,] 6 6
[7,] 7 7
[8,] 8 8
[9,] 1 9
[10,] 2 1
[11,] 3 2
[12,] 4 3
instead of the following (expected) result:
[[1]]
[,1] [,2] [,3]
[1,] 1 5 1
[2,] 2 6 2
[3,] 3 7 3
[4,] 4 8 4
[[2]]
[,1] [,2] [,3] [,4]
[1,] 1 4 7 1
[2,] 2 5 8 2
[3,] 3 6 9 3
Can anybody help me in understanding if something in my code is wrong? Thank you!
Make sure to set SIMPLIFY to false
mapply(a,b,FUN=cbind, SIMPLIFY=FALSE)
otherwise mapply tries to coerce everything into a compatible single result. In your case, because the return from each call had 12 elements, it put those two elements side by side in a matrix, with the first matrix values in the first column, and the second matrix in the second column.
Alternatively you can use
Map(cbind, a, b)
which always returns a list. (Map is also nice because if a has names it will use those names in the resulting list which isn't useful in this case, but may be useful in others.)
This question already has answers here:
Change row order in a matrix/dataframe
(7 answers)
Closed 5 years ago.
I have a simple matrix like:
> a = matrix(c(c(1:10),c(10:1)), ncol=2)
> a
[,1] [,2]
[1,] 1 10
[2,] 2 9
[3,] 3 8
[4,] 4 7
[5,] 5 6
[6,] 6 5
[7,] 7 4
[8,] 8 3
[9,] 9 2
[10,] 10 1
I would like to get this result:
[,1] [,2]
[1,] 10 1
[2,] 9 2
[3,] 8 3
[4,] 7 4
[5,] 6 5
[6,] 5 6
[7,] 4 7
[8,] 3 8
[9,] 2 9
[10,] 1 10
The exact reverse of the matrix. How can I get it?
Thanks
a[nrow(a):1,]
# [,1] [,2]
# [1,] 10 1
# [2,] 9 2
# [3,] 8 3
# [4,] 7 4
# [5,] 6 5
# [6,] 5 6
# [7,] 4 7
# [8,] 3 8
# [9,] 2 9
# [10,] 1 10
Try rev with apply:
> a <- matrix(c(1:10,10:1), ncol=2)
> a
[,1] [,2]
[1,] 1 10
[2,] 2 9
[3,] 3 8
[4,] 4 7
[5,] 5 6
[6,] 6 5
[7,] 7 4
[8,] 8 3
[9,] 9 2
[10,] 10 1
> b <- apply(a, 2, rev)
> b
[,1] [,2]
[1,] 10 1
[2,] 9 2
[3,] 8 3
[4,] 7 4
[5,] 6 5
[6,] 5 6
[7,] 4 7
[8,] 3 8
[9,] 2 9
[10,] 1 10
Here's one way:
a[, rev(seq_len(ncol(a)))]
[,1] [,2]
[1,] 10 1
[2,] 9 2
[3,] 8 3
[4,] 7 4
[5,] 6 5
[6,] 5 6
[7,] 4 7
[8,] 3 8
[9,] 2 9
[10,] 1 10