Insert row at a specific location in matrix using R

烈酒焚心 提交于 2021-01-27 05:11:35

问题


I'm trying to add rows to a matrix at particular locations, where the locations are contained in a vector. The schema below shows the inputs and the expected outcome. I tried to use a "for" loop but couldn't make it work. Any suggestions help.

Source Matrix (6x3)

(1) 1   2   3
(2) 4   5   6
(3) 7   8   9
(4) 6   9   2
(5) 3   6   1
(6) 2   2   7

Vector of locations (indicates the row in the source matrix that will contain zeros)

[2, 5, 6]  

Result Matrix(6+length.vector x 3)

(1) 1   2   3
(2*)0   0   0
(3) 4   5   6
(4) 7   8   9
(5*)0   0   0
(6*)0   0   0
(7) 6   9   2
(8) 3   6   1
(9) 2   2   7

回答1:


mat <- matrix(1:18,6)
vec <- c(2, 5, 6)

# New matrix 'new_mat' with all zeros, 
# No. of rows = original matrix rows + number new rows to be added
new_mat <- matrix(0,nrow=9,ncol=3)  

# 'new_mat' rows getting filled with `mat` values
new_mat[-vec,] <- mat   
new_mat
#      [,1] [,2] [,3]
# [1,]    1    7   13
# [2,]    0    0    0
# [3,]    2    8   14
# [4,]    3    9   15
# [5,]    0    0    0
# [6,]    0    0    0
# [7,]    4   10   16
# [8,]    5   11   17
# [9,]    6   12   18



回答2:


Here's one approach:

## inputs
m <- matrix(c(1,2,3,4,5,6,7,8,9,6,9,2,3,6,1,2,2,7),ncol=3L,byrow=T);
i <- c(2L,5L,6L);

## solution
ris <- integer(nrow(m)+length(i)); ris[i] <- nrow(m)+1L; ris[-i] <- seq_len(nrow(m));
rbind(m,0)[ris,];
##       [,1] [,2] [,3]
##  [1,]    1    2    3
##  [2,]    0    0    0
##  [3,]    4    5    6
##  [4,]    7    8    9
##  [5,]    0    0    0
##  [6,]    0    0    0
##  [7,]    6    9    2
##  [8,]    3    6    1
##  [9,]    2    2    7

It works by first computing a vector of row indexes ris. The vector is first allocated to the required size, which is the number of rows in the original matrix nrow(m) plus the number of rows to be inserted, which is length(i). Then the insertion indexes are assigned to an index just outside the number of rows in the original matrix nrow(m)+1L, and the non-insertion indexes are assigned to the sequence seq_len(nrow(m)). We can attain the required output by indexing with ris a composite matrix that consists of the original matrix row-bound with a row of zeroes (or whatever set of values is desired to be inserted at each insertion index).




回答3:


This one is similar to @bgoldst's answer, just a bit different. It adds a row of zeros and then reorders the rows. We start with a vector of nrow(m) + 1 of length nrow(m) + length(x) (where x is the location vector) , and then replace the indices that are not in x with their respective original indices.

nr <- nrow(m)
rbind(m, 0)[replace(rep(nr + 1L, nr + length(x)), -x, seq_len(nr)), ]
#      [,1] [,2] [,3]
# [1,]    1    2    3
# [2,]    0    0    0
# [3,]    4    5    6
# [4,]    7    8    9
# [5,]    0    0    0
# [6,]    0    0    0
# [7,]    6    9    2
# [8,]    3    6    1
# [9,]    2    2    7

Data:

m <- structure(c(1L, 4L, 7L, 6L, 3L, 2L, 2L, 5L, 8L, 9L, 6L, 2L, 3L, 
6L, 9L, 2L, 1L, 7L), .Dim = c(6L, 3L))
x <- c(2, 5, 6)


来源:https://stackoverflow.com/questions/37625471/insert-row-at-a-specific-location-in-matrix-using-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!