If I had a matrix like:
[,1] [,2]
[1,] 1 7
[2,] 2 8
[3,] 3 9
[4,] 4 10
[5,] 5 11
[6,] 6 12
Does anyone have an idea as to how I might create a new matrix from the above that looks like:
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 7 3 9 5 11
[2,] 2 8 4 10 6 12
We create a grouping variable with ?gl and use the arguments n=nrow(m1), k=2 and length=nrow(m1). We split the matrix ('m1'), unlist, and create a new matrix with nrow=2.
matrix(unlist(split(m1,as.numeric(gl(nrow(m1), 2, nrow(m1))))),nrow=2)
# [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] 1 7 3 9 5 11
#[2,] 2 8 4 10 6 12
Or another option is converting to array by specifying the dimensions. Here I used c(2, 2, 3) as we can get a 2x2 matrix for the first two dimensions and the third is based on the nrow(m1)/2. Then, we can permute the dimensions of the array using aperm, concatenate (c) to form a vector and convert to matrix.
matrix(c(aperm(array(t(m1), c(2, 2,3)),c(2,1,3))), nrow=2)
# [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] 1 7 3 9 5 11
#[2,] 2 8 4 10 6 12
data
m1 <- structure(1:12, .Dim = c(6L, 2L))
Here' another option: First the matrix is transformed into one with two rows, then the odd and even numbered columns are rearranged:
m3 <- m2 <- matrix(c(m),nrow = 2) #take data from original matrix, convert it into a matrix with two rows and store a copy in m2 and m3
m3[,seq(1,ncol(m2),2)] <- m2[,1:(ncol(m2)/2)] #define the odd-numbered columns of m3
m3[,seq(2,ncol(m2),2)] <- m2[,(ncol(m2)/2+1):ncol(m2)] # same for the even-numbered columns
> m3
# [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] 1 7 3 9 5 11
#[2,] 2 8 4 10 6 12
Related
"I have a very long matrix, measuring 30^5 x 3 entries. I basically consists of subblocks of 10.000 30 x 3 matrices, stacked on top of one another. I want to afficiently "cbind" them, next to one another (without looping constructs), leading to a 30 x 30^4 matrix.
Just changing the matrix dimensions does not work, as R fills the new matrix per individual column.
I'm sure there is a very compact, superefficient way of doing this, and I'll slap myself on the forehead as soon as you fill me in on the obvious solution.
Thanks!"
"Just changing the matrix dimensions does not work, as R fills the new matrix per individual column."
```R
test <- matrix(c(1:18), 6, 3, byrow = FALSE)
>test
[,1] [,2] [,3]
[1,] 1 7 13
[2,] 2 8 14
[3,] 3 9 15
[4,] 4 10 16
[5,] 5 11 17
[6,] 6 12 18
dim(test) <- c(3,6)
>test
[,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
```
The output I'm looking for is:
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 7 13 4 10 16
[2,] 2 8 14 5 11 17
[3,] 3 9 15 6 12 18
We can create a grouping variable to split the sequence of rows, subset the matrix and then cbind
do.call(cbind, lapply(split(seq_len(nrow(test)),
as.integer(gl(nrow(test), 3, nrow(test)))), function(i) test[i,]))
# [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] 1 7 13 4 10 16
#[2,] 2 8 14 5 11 17
#[3,] 3 9 15 6 12 18
I have this vector
b=c(5,8,9)
I want to perform a combination on b selecting 2 items at a time such that i have the original elements of b as my first row to get
[,1] [,2] [,3]
[1,] 5 8 9
[2,] 8 9 5
I tried combn(b, 2) and it gives me this
[,1] [,2] [,3]
[1,] 5 5 8
[2,] 8 9 9
Can i get help to achieve my desired result?
Since the second row of your desired result is not uniquely defined, there is no need for any sophisticated tools:
b <- 1:10
rbind(b, c(b[-1], b[1]))
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# b 1 2 3 4 5 6 7 8 9 10
# 2 3 4 5 6 7 8 9 10 1
In this case I only "shift" b by one position in the second row, which indeed results in a permutation. I'm assuming that the elements of b don't repeat.
Let's say I have the below matrix:
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
I want to generate a matrix which is the concatenation (by column) of matrices that are generated by repetition of each column k times. For example, when k=3, below is what I want to get:
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 1 1 2 2 2
[2,] 3 3 3 4 4 4
[3,] 5 5 5 6 6 6
How can I do that without a for loop?
You can do this with column indexing. A convenient way to repeat each column number the correct number of times is the rep function:
mat[,rep(seq_len(ncol(mat)), each=3)]
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 1 1 1 2 2 2
# [2,] 3 3 3 4 4 4
# [3,] 5 5 5 6 6 6
In the above expression, seq_len(ncol(mat)) is the sequence from 1 through the number of columns in the matrix (you could think of it like 1:ncol(mat), except it deals nicely with some special cases like 0-column matrices).
Data:
(mat <- matrix(1:6, nrow=3, byrow = TRUE))
# [,1] [,2]
# [1,] 1 2
# [2,] 3 4
# [3,] 5 6
We can repeat each element of matrix k times and fit the vector in a matrix where number of columns is k times the original one.
k <- 3
matrix(rep(t(mat), each = k), ncol = ncol(mat) * k, byrow = TRUE)
# [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] 1 1 1 2 2 2
#[2,] 3 3 3 4 4 4
#[3,] 5 5 5 6 6 6
I have a matrix of type numeric, with dim 10000 * 50. Now I want to find the index of top 5 elements in every row in the order of their values. e.g. a sample might look like :
set.seed(2)
v1 <- matrix(runif(20 , 0 ,20 ) , 2 ,10)
v1
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#[1,] 3.697645 11.466527 18.87679 2.58318 9.36037 11.053481 15.210266 8.105644 19.527970 8.896185
#[2,] 14.047481 3.361038 18.86950 16.66898 10.99967 4.777895 3.616402 17.070969 4.516509 1.499588
Then I want the output to look like :
#[1,] 9 3 7 2 6
#[2,] 3 8 4 1 5
I could find only this question, which explains how to find top n elements, but not in the order of values.
apply() is perfect for row-wise operations on matrices. You could do
t(apply(v1, 1, function(x) order(-x)[1:5]))
# [,1] [,2] [,3] [,4] [,5]
# [1,] 9 3 7 2 6
# [2,] 3 8 4 1 5
This runs the order() function row-wise down the matrix v1 then takes the first five values for each one, transposing the result since you specify rows not columns.
This can also be done with data.table after melting into 'long' format, grouped by 'Var1', we get the order of 'value'
library(reshape2)
library(data.table)
setDT(melt(v1))[, head(order(-value),5), Var1]
# Var1 V1
#1: 1 9
#2: 1 3
#3: 1 7
#4: 1 2
#5: 1 6
#6: 2 3
#7: 2 8
#8: 2 4
#9: 2 1
#10: 2 5
Or using base R
ave(-v1, row(v1), FUN = order)[,1:5]
# [,1] [,2] [,3] [,4] [,5]
#[1,] 9 3 7 2 6
#[2,] 3 8 4 1 5
I want to extract every nth element of row for each row in a matrix, here is my code:
x <- matrix(1:16,nrow=2)
x
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1 3 5 7 9 11 13 15
[2,] 2 4 6 8 10 12 14 16
I have tried:
sapply(x, function(l) x[seq(1,8,2)])
which clearly fails.
I want to pull every 2nd value from "x" the desired output would be something like...
[,1] [,2] [,3] [,4]
[1,] 3 7 11 15
[2,] 4 8 12 16
You are overcomplicating it:
This gives you what you need
x[,seq(2, 8, 2)]
or, more generally
x[,seq(2, ncol(x), 2)]