如何添加列来展开R中的矩阵

时间:2022-05-28 14:59:05

Sorry I couldn't think of a more informative title, but here's my challenge. I have a matrix and I need to add columns in specific places based on parameters described by a vector. For example, if I have the following matrix:

对不起,我想不出一个更能提供信息的标题,但这是我的挑战。我有一个矩阵,我需要根据向量描述的参数在特定的地方添加列。例如,如果我有以下矩阵:

1, 0, 1, 2, 0
0, 0, 1, 1, 1
1, 1, 0, 0, 0
2, 0, 1, 0, 2

but for a particular R package (unmarked), I need to add columns of NA in specific place. I have a vector relating the columns in the matrix:

但是对于一个特定的R包(未标记),我需要在特定的位置添加NA列。我有一个向量关于矩阵中的列:

1, 1, 1, 2, 3

Which indicates that columns 1-3 were from the same sampling period and columns 4 and 5 were from different sampling periods. I need to make the number of columns in the matrix equal the max number from the same sampling period times the number of sampling periods. In this case there are three 1s (max number of any unique value in the vector) and a total of three sampling periods (max number in the vector). So I need a matrix with 9 columns (3 x 3). Specifically, I need to add the new columns of NAs after the 4th and 5th columns. Basically, I just need columns of NAs to be placeholders to have a matrix where the number of observations (each column) is the same (=3) for each of the sample periods (indicated by the number in the vector). This is difficult to describe but in this imaginary example I would want to end up with:

这表明,第1-3列来自同一采样周期,第4和第5列来自不同的采样周期。我需要让矩阵中的列数等于同一采样周期的最大值乘以采样周期的个数。在这种情况下,有3个1(向量中任何唯一值的最大值)和3个采样周期的总数(在向量中是最大的数)。所以我需要一个包含9列(3×3)的矩阵,具体来说,我需要在第4和第5列之后添加新的NAs列。基本上,我只需要NAs的列作为占位符,就可以得到一个矩阵,其中每一列的观测次数(每一列)对于每个样本周期(由向量中的数字表示)都是相同的(=3)。这很难描述,但在这个假想的例子中,我想以:

1, 0, 1, 2, NA, NA, 0, NA, NA
0, 0, 1, 1, NA, NA, 1, NA, NA
1, 1, 0, 0, NA, NA, 0, NA, NA
2, 0, 1, 0, NA, NA, 2, NA, NA

this would be described by a vector that looked like:

这可以用这样一个向量来描述:

1, 1, 1, 2, 2, 2, 3, 3, 3

although I don't actually need to produce that vector, just the matrix. Obviously, it was easy to add those columns in this case, but for my data I have a much bigger matrix that will end up with ~200 columns. Plus I will likely have to do this for numerous data sets.

虽然我不需要生成这个向量,只需要生成矩阵。显然,在这种情况下,很容易添加这些列,但是对于我的数据,我有一个大得多的矩阵,它最终会有大约200列。另外,对于很多数据集,我可能需要这样做。

Can anyone help me with a way to code this in R so that I can automate the process of expanding the matrix?

有没有人能帮我在R中编码这个,这样我就能自动展开矩阵了?

Thanks for any advice or suggestions!

谢谢您的建议!


EDIT: to make things a bit more similar to my actual data here is a reproducible matrix and vector similar to my current ones:

编辑:为了使事情更接近我的实际数据,这里有一个可复制的矩阵和向量,类似于我目前的数据:

    m <- matrix(rpois(120*26, 1), nrow = 120, ncol = 26)
    v <- c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 5, 5, 6, 6, 6, 6, 7)

2 个解决方案

#1


4  

Assuming m is the matrix and v is the vector, you can use something like

假设m是矩阵,v是向量,你可以用类似的东西

 t = table(v)
 size = dim(m)[1] * max(t)   # size of each block based on the longest
 matrix(unlist(lapply(names(t), function(i) {
               x = m[, v == i]                  # get the short block
               c(x, rep(NA, size - length(x)))  # extend it to size
         })), dim(m)[1])

#2


1  

To modify the matrix just as you asked assuming the matrix is mat:

按照你的要求修改矩阵假设矩阵是mat:

nr <- nrow(mat)
nas <- rep(NA, nr)
l <- lapply( 4:ncol(mat), function(x) matrix(c(mat[,x],nas,nas), nrow = nr) )
cbind(mat[,1:3], do.call(cbind,l))

#1


4  

Assuming m is the matrix and v is the vector, you can use something like

假设m是矩阵,v是向量,你可以用类似的东西

 t = table(v)
 size = dim(m)[1] * max(t)   # size of each block based on the longest
 matrix(unlist(lapply(names(t), function(i) {
               x = m[, v == i]                  # get the short block
               c(x, rep(NA, size - length(x)))  # extend it to size
         })), dim(m)[1])

#2


1  

To modify the matrix just as you asked assuming the matrix is mat:

按照你的要求修改矩阵假设矩阵是mat:

nr <- nrow(mat)
nas <- rep(NA, nr)
l <- lapply( 4:ncol(mat), function(x) matrix(c(mat[,x],nas,nas), nrow = nr) )
cbind(mat[,1:3], do.call(cbind,l))