How to permute rows of a given matrix - r

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),]

Related

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

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

Sorting specified rows in a matrix by the first column in R

I have a character matrix mtr of n rows and 3 columns.
I have a numeric vector nmb with some numbers, for example, 4,5,6
I want to sort only the rows of mtr, the numbers of which are contained by nmb, by the first column of my matrix.
So in my case I want to leave my matrix untouched except for rows 4,5,6 which I would like to be sorted by the first column and, of course, written back into my matrix mtr.
How could I do that? Thanks.
You can do it in this way:
mtr[nmb,] <- mtr[order(mtr[nmb,1]),]
I think this will do it
mtr[nmb,] <- mtr[nmb,][order(mtr[nmb,1]),]
An example:
nmb <- 4:6
mtr <- matrix(30:1, ncol=3)
> mtr
[,1] [,2] [,3]
[1,] 30 20 10
[2,] 29 19 9
[3,] 28 18 8
[4,] 27 17 7
[5,] 26 16 6
[6,] 25 15 5
[7,] 24 14 4
[8,] 23 13 3
[9,] 22 12 2
[10,] 21 11 1
> mtr[nmb,] <- mtr[nmb,][order(mtr[nmb,1]),]
> mtr
[,1] [,2] [,3]
[1,] 30 20 10
[2,] 29 19 9
[3,] 28 18 8
[4,] 25 15 5 <-
[5,] 26 16 6 <- sorted
[6,] 27 17 7 <-
[7,] 24 14 4
[8,] 23 13 3
[9,] 22 12 2
[10,] 21 11 1

Delete row based on the value of the rows above

I have a the following data set:
data <- cbind(c(1,2,3,4,5,6,7,8,9,10,11),c(1,11,21,60,30,2,61,12,3,35,63))
I would like to select the rows for which the number in the second column is greater than the highest number reached up to that point. The result should look like this.
[,1] [,2]
[1,] 1 1
[2,] 2 11
[3,] 3 21
[4,] 4 60
[5,] 7 61
[6,] 11 63
You want to try cummax:
> d[ d[,2] == cummax(d[,2]) ,]
[,1] [,2]
[1,] 1 1
[2,] 2 11
[3,] 3 21
[4,] 4 60
[5,] 7 61
[6,] 11 63
PS. data is an internal R function, so, since R variables and functions share the namespace (R design was influenced by Scheme, which is a "Lisp-1"), your variable shadows the system function.
The cummax function should work well
data[ data[,2]==cummax(data[,2]),]
returns
[,1] [,2]
[1,] 1 1
[2,] 2 11
[3,] 3 21
[4,] 4 60
[5,] 7 61
[6,] 11 63
as desired.

How to reverse a matrix in R? [duplicate]

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

Resources