How to reverse a matrix in R? [duplicate] - r

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

Related

How to permute rows of a given matrix

Good morning !
Assume we have the following matrix :
m=matrix(1:18,ncol=2)
print("m : before")
print(m)
[1] "m : before"
[,1] [,2]
[1,] 1 10
[2,] 2 11
[3,] 3 12
[4,] 4 13
[5,] 5 14
[6,] 6 15
[7,] 7 16
[8,] 8 17
[9,] 9 18
As an example, I'm wanting to permute a number of rows :
tmp=m[8:9,]
m[8:9,]=m[3:4,]
m[3:4,]=tmp
This is the same as :
# indices to permute
before=8:9
after=3:4
tmp=m[before,]
m[before,]=m[after,]
m[after,]=tmp
[1] "after"
[,1] [,2]
[1,] 1 10
[2,] 2 11
[3,] 8 17
[4,] 9 18
[5,] 5 14
[6,] 6 15
[7,] 7 16
[8,] 3 12
[9,] 4 13
I'm wanting to know if there is any package that automatize such task. For the moment , I'm not willing to use a user-defined function.
Thank you for help !
I think the simplest solution is just to use base r function, like sample:
set.seed(4)
m[sample(1:nrow(m),nrow(m)),]
which give you:
[,1] [,2]
[1,] 8 17
[2,] 3 12
[3,] 9 18
[4,] 7 16
[5,] 4 13
[6,] 6 15
[7,] 2 11
[8,] 1 10
[9,] 5 14
If you want to permute just some rows you can do :
m[7:9,] <- m[sample(7:9,3),]#where the last number (3) is the number of row
to permute
which give you
[,1] [,2]
[1,] 1 10
[2,] 2 11
[3,] 3 12
[4,] 4 13
[5,] 5 14
[6,] 6 15
[7,] 7 16
[8,] 9 18
[9,] 8 17
Just try to exchange the index order.
m[c(before,after),] = m[c(after,before),]

How to rbind a single number with a matrix in R

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

unique relation between two columns X and Y using R [duplicate]

I have a data frame of integers that is a subset of all of the n choose 3 combinations of 1...n.
E.g., for n=5, it is something like:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 4
[3,] 1 2 5
[4,] 1 3 4
[5,] 1 3 5
[6,] 1 4 5
[7,] 2 1 3
[8,] 2 1 4
[9,] 2 1 5
[10,] 2 3 4
[11,] 2 3 5
[12,] 2 4 5
[13,] 3 1 2
[14,] 3 1 4
[15,] 3 1 5
[16,] 3 2 4
[17,] 3 2 5
[18,] 3 4 5
[19,] 4 1 2
[20,] 4 1 3
[21,] 4 1 5
[22,] 4 2 3
[23,] 4 2 5
[24,] 4 3 5
[25,] 5 1 2
[26,] 5 1 3
[27,] 5 1 4
[28,] 5 2 3
[29,] 5 2 4
[30,] 5 3 4
What I'd like to do is remove any rows with duplicate combinations, irrespective of ordering. E.g., [1,] 1 2 3 is the same as [1,] 2 1 3 is the same as [1,] 3 1 2.
unique, duplicated, &c. don't seem to take this into account. Also, I am working with quite a large amount of data (n is ~750), so it ought to be a pretty fast operation. Are there any base functions or packages that can do this?
Sort within the rows first, then use duplicated, see below:
# example data
dat = matrix(scan('data.txt'), ncol = 3, byrow = TRUE)
# Read 90 items
dat[ !duplicated(apply(dat, 1, sort), MARGIN = 2), ]
# [,1] [,2] [,3]
# [1,] 1 2 3
# [2,] 1 2 4
# [3,] 1 2 5
# [4,] 1 3 4
# [5,] 1 3 5
# [6,] 1 4 5
# [7,] 2 3 4
# [8,] 2 3 5
# [9,] 2 4 5
# [10,] 3 4 5

Iterating a 2D matrix in chunks using R

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

Removing duplicate combinations (irrespective of order)

I have a data frame of integers that is a subset of all of the n choose 3 combinations of 1...n.
E.g., for n=5, it is something like:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 4
[3,] 1 2 5
[4,] 1 3 4
[5,] 1 3 5
[6,] 1 4 5
[7,] 2 1 3
[8,] 2 1 4
[9,] 2 1 5
[10,] 2 3 4
[11,] 2 3 5
[12,] 2 4 5
[13,] 3 1 2
[14,] 3 1 4
[15,] 3 1 5
[16,] 3 2 4
[17,] 3 2 5
[18,] 3 4 5
[19,] 4 1 2
[20,] 4 1 3
[21,] 4 1 5
[22,] 4 2 3
[23,] 4 2 5
[24,] 4 3 5
[25,] 5 1 2
[26,] 5 1 3
[27,] 5 1 4
[28,] 5 2 3
[29,] 5 2 4
[30,] 5 3 4
What I'd like to do is remove any rows with duplicate combinations, irrespective of ordering. E.g., [1,] 1 2 3 is the same as [1,] 2 1 3 is the same as [1,] 3 1 2.
unique, duplicated, &c. don't seem to take this into account. Also, I am working with quite a large amount of data (n is ~750), so it ought to be a pretty fast operation. Are there any base functions or packages that can do this?
Sort within the rows first, then use duplicated, see below:
# example data
dat = matrix(scan('data.txt'), ncol = 3, byrow = TRUE)
# Read 90 items
dat[ !duplicated(apply(dat, 1, sort), MARGIN = 2), ]
# [,1] [,2] [,3]
# [1,] 1 2 3
# [2,] 1 2 4
# [3,] 1 2 5
# [4,] 1 3 4
# [5,] 1 3 5
# [6,] 1 4 5
# [7,] 2 3 4
# [8,] 2 3 5
# [9,] 2 4 5
# [10,] 3 4 5

Resources