Transform 87x2 matrix into 29x6 in R - r

Suppose if I have a matrix with dimension 87x2. How can I convert into the dimension 29x6 in r
set.seed(1)
mat1 = matrix(runif(174), 87, 2)
I wanted to have like this below
> matrix(c(1:12), 6, 2)
[,1] [,2]
[1,] 1 7
[2,] 2 8
[3,] 3 9
[4,] 4 10
[5,] 5 11
[6,] 6 12
> matrix(c(1:12), 2, 6)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 3 5 7 9 11
[2,] 2 4 6 8 10 12
Thank you in advance.

You can do the following:
mat1 <- matrix(c(1:12), 6, 2)
matrix(mat1, nrow = 2, ncol = 6)
# [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] 1 3 5 7 9 11
#[2,] 2 4 6 8 10 12
Or set the dimensions directly using dim
dim(mat1) <- c(2, 6)

Related

In R, what does byrow do exactly?

I am new to R. I understand if for instance I have a matrix I am interested in building:
a <- matrix(c(1,2,3,4,5,6,7,8), nrow=2)
# [,1] [,2] [,3] [,4]
# [1,] 1 3 5 7
# [2,] 2 4 6 8
what exactly does byrow do differently in the example below? that is what I am having difficulty understanding
a <- matrix(c(1,2,3,4,5,6,7,8), nr=2, byrow = T)
# [,1] [,2] [,3] [,4]
# [1,] 1 2 3 4
# [2,] 5 6 7 8

Sample from a matrix and split sampled and non sampled values in R

I want to sample 'n' rows from a matrix:
data <- matrix(data = 1:12, nrow = 4, ncol = 3)
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
I use the following code :
selection <-sample(nrow(data),size = 2, replace = FALSE)
data[selection,]
[,1] [,2] [,3]
[1,] 4 8 12
[2,] 3 7 11
Is there a way I could return a matrix containing only the rows that haven't been sampled? In this case:
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
Many thanks in advance.

Reducing a 3d array to 2d

Is there a more succinct, one-liner way to do the following?
x <- array(1:12, dim = c(3, 2, 2))
> x[1,,]
[,1] [,2]
[1,] 1 7
[2,] 4 10
> x[2,,]
[,1] [,2]
[1,] 2 8
[2,] 5 11
> x[3,,]
[,1] [,2]
[1,] 3 9
[2,] 6 12
# Reduce 3d array to 2d (Is there a more elegant way?)
y <- x
dim(y) <- c(nrow(y), 4)
> y
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
You can just feed your original array to the array constructor again, and use dim to get the dimension you want to preserve:
y <- array(x, dim = c(dim(x)[1], 4))
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
If you wanted a version that doesn't rely on hardcoding the number of columns:
y <- array(x, dim = c(dim(x)[1], dim(x)[2] * dim(x)[3]))

Create new columns based on rows

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

sumcum on matrix using R

I would like to make a cumsum of multiple matrix obtaining the steps. If we consider:
A <- structure(c(1, 2, 3, 2, 3, 1, 4, 1, 2), .Dim = c(3, 3))
# [,1] [,2] [,3]
# [1,] 1 2 4
# [2,] 2 3 1
# [3,] 3 1 2
B <- structure(c(6, 1, 9, 6, 3, 7, 3, 2, 8), .Dim = c(3, 3))
# [,1] [,2] [,3]
# [1,] 6 6 3
# [2,] 1 3 2
# [3,] 9 7 8
C <- structure(c(1, 1, 2, 5, 3, 3, 3, 9, 1), .Dim = c(3, 3))
# [,1] [,2] [,3]
# [1,] 1 5 3
# [2,] 1 3 9
# [3,] 2 3 1
I would like the following results:
[,1] [,2] [,3]
[1,] 1 2 4
[2,] 2 3 1
[3,] 3 1 2
[,1] [,2] [,3]
[1,] 7 8 7
[2,] 3 6 3
[3,] 12 8 10
[,1] [,2] [,3]
[1,] 8 13 10
[2,] 4 9 12
[3,] 14 11 11
with all steps! I could do this with a for loop, but it's slow with big matrix, how can I do this with apply ?
This is a perfect job for Reduce:
Reduce("+", list(A,B,C), accumulate=TRUE)
[[1]]
[,1] [,2] [,3]
[1,] 1 2 4
[2,] 2 3 1
[3,] 3 1 2
[[2]]
[,1] [,2] [,3]
[1,] 7 8 7
[2,] 3 6 3
[3,] 12 8 10
[[3]]
[,1] [,2] [,3]
[1,] 8 13 10
[2,] 4 9 12
[3,] 14 11 11

Resources