how to stack groups of columns into a new matrix in r?

user511792

I have a 3d array in R, say the dimension is 500*3*5, call it data. So there are 5 slices of matrices, each has 500 rows and 3 columns. Now I want to create a new matrix that stacks the 5 slices vertically. So the output has 500*5 rows and 5 columns and it looks like this:

[dim1]   [ dim3 ]       [1]       [2]        [3]       

 1          1           data     data        data
 1          2           data     data        data
 1          3           data     data        data
 1          4           data     data        data
 1          5           data     data        data
 2
 2
 2
 ..
 500          1           data     data        data
 500          2           data     data        data
 500          3           data     data        data
 500          4           data     data        data
 500          5           data     data        data

So for each 500 rows, repeat the 2nd and 3rd dimension of the original array, such that column1 repeats the original row index, column2 repeats the old z dimensional index and column 3-5 are the 3 columns of data (numeric and have missing values). i just used the number 5 here, in my data the number is much bigger, so i need to be able to do this more generally. Thanks!!!

flodel

Let a be your 3d array, you can do:

n <- dim(a)
b <- aperm(a, c(3,1,2))
dim(b) <- c(n[3] * n[1], n[2])

If you want to add the dim indices to the left, you can do:

b <- cbind(rev(expand.grid(dim3 = 1:n[3], dim1 = 1:n[1])), b)   

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to stack groups of columns in and pandas dataframe?

How to stack elements of a list (dataframes or vectors) by rows inside a matrix, including the names of the columns inside the matrix in R?

How I can compare in R two columns of a matrix with two others and generate at the same time a new matrix?

How to scatterplot columns of a matrix in R?

R how to stack image from their matrix value

How to perform functions on groups of columns in R

Generate vector that groups (by name) columns in a new vector in R

How to create a new matrix by subtracting columns in a given matrix?

How to compare two columns of matrix in r and output the column name in a new vector

How to stack multiple columns into one using R

how to change the order of columns in a matrix in R

How to graph columns of a matrix against the rows in R?

How to select unique columns in an R matrix

How to extracting consecutive columns from a matrix in R?

How to stack raw and creating a new variable in R

Extracting multiple columns across matricies to a new matrix in R

how to query different columns and stack into new table in google sheet

How to vertically stack every other columns of a dataframe into a new column

Creating new columns by adding groups of columns

Octave function to get groups of consecutive columns in matrix

Efficiently combine (AND) groups of columns in a logical matrix

How to add numpy matrix as new columns for pandas dataframe?

How to change matrix rows or columns names by a list of existing names to new?

How to convert times series raster stack to matrix or large list in r?

How to melt two groups of data into two columns in R?

How to do a test to all sequential pairs of columns by groups in R

R: how to apply a function to some columns into groups of data?

How find maximum values for two columns separately for groups in R?

How to divide into 4 equal groups across multiple columns in R