I have a very large dataset with a size of 60K x 4 K I am trying to add every four values โโin a row in each column of a row. The following is an example dataset.
set.seed(123) mat <- matrix (sample(0:1, 48, replace = TRUE), 4) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,] 0 1 1 1 0 1 1 0 1 1 0 0 [2,] 1 0 0 1 0 1 1 0 1 0 0 0 [3,] 0 1 1 0 0 1 1 1 0 0 0 0 [4,] 1 1 0 1 1 1 1 1 0 0 0 0
Here is what I am trying to accomplish:
mat[1,1] + mat[1,2] + mat[1,3] + mat[1,4] = 0 + 1 + 1 + 1 = 3
i.e. add every four values โโand output.
mat[1,5] + mat[1,6] + mat[1,7] + mat[1,8] = 0 + 1 + 1 + 0 = 2
Continue to complete the matrix (up to 12 here).
mat[1,9] + mat[1,10] + mat[1,11] + mat[1,12]
Once the first line is completed, apply it to the second line, for example:
mat[2,1] + mat[2,2] + mat[2,3] + mat[2,4] mat[2,5] + mat[2,6] + mat[2,7] + mat[2,8] mat[2,9] + mat[2,10] + mat[2,11] + mat[2,12]
The result will be nrow x (ncol)/4 .
The expected result will look like this:
col1-col4 col5-8 col9-12 row1 3 2 2 row2 2 2 1 row3 2 3 0 row4 3 4 0
Similarly, for row 3, the number of rows in the matrix. How can I loop this efficiently.