简体   繁体   English

如何从R中矩阵列表中的每个矩阵中删除列?

[英]How to remove columns from each matrix in a list of matrices in R?

I am not very familiar with using the list function in R. This is my first use of a list of matrices. 我不太熟悉在R中使用list函数。这是我第一次使用矩阵列表。 I am trying to remove the same columns from each matrix in a list of matrices but I am not sure how this works with the indexing in R. 我试图从矩阵列表中的每个矩阵中删除相同的列,但我不确定它如何与R中的索引一起工作。

Right now I have 8 matrices in a list. 现在我在列表中有8个矩阵。 Each matrix is [120, 56]. 每个矩阵是[120,56]。 I would like to remove rows columns 17-40 and 49-56 from each matrix. 我想从每个矩阵中删除 17-40和49-56 Therefore, I would end up with a list of 8 matrices of [120, 24]. 因此,我最终得到了[120,24]的8个矩阵的列表。

Here is an example of the matrix list I have: 这是我有一个矩阵列表的例子:

MatrixList <- list(maxT = matrix(1:56, 120, 56, byrow = TRUE),
        minT = matrix(1:56, 120, 56, byrow = TRUE),
        meanT = matrix(1:56, 120, 56, byrow = TRUE),
        rain24 = matrix(1:56, 120, 56, byrow = TRUE),
        rain5d = matrix(1:56, 120, 56, byrow = TRUE),
        maxT2 = matrix(1:56, 120, 56, byrow = TRUE),
        minT2 = matrix(1:56, 120, 56, byrow = TRUE),
        meanT2 = matrix(1:56, 120, 56, byrow = TRUE))

I know this seems like a simple problem but I'm a novice and am just not sure how to use a combination of for loops and internal indexing to remove columns. 我知道这似乎是一个简单的问题,但我是一个新手,我只是不确定如何使用for循环和内部索引的组合来删除列。 I'd rather learn how to do this efficiently, rather than doing it for each matrix individually and then creating the list. 我宁愿学习如何有效地做到这一点,而不是单独为每个矩阵做这个,然后创建列表。

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

As is often the case, @DWin gets in early with an excellent answer. 通常情况下,@ DWin很早就得到了一个很好的答案。 Here is an alternative that my simple mind finds easier to comprehend. 这是我的简单头脑更易于理解的替代方案。

You can use lapply to traverse your list, and then standard subsetting using the [ operator. 您可以使用lapply遍历列表,然后使用[运算符]进行标准子集化。

Rather than using [ operator as a function (as @DWin suggests), I prefer writing an anonymous function inside lapply that looks exactly like the operation you would perform to transform a single element of your list (ie subset a single matrix): 我不喜欢使用[ operator作为函数(如@DWin建议的),而是更喜欢在lapply中编写一个匿名函数,它看起来与您为了转换列表中的单个元素(即子集单个矩阵)所执行的操作完全相同:

mls <- lapply(MatrixList, function(x)x[-c(17:40, 49:56), ])
str(mls)

List of 8
 $ maxT  : int [1:88, 1:56] 1 1 1 1 1 1 1 1 1 1 ...
 $ minT  : int [1:88, 1:56] 1 1 1 1 1 1 1 1 1 1 ...
 $ meanT : int [1:88, 1:56] 1 1 1 1 1 1 1 1 1 1 ...
 $ rain24: int [1:88, 1:56] 1 1 1 1 1 1 1 1 1 1 ...
 $ rain5d: int [1:88, 1:56] 1 1 1 1 1 1 1 1 1 1 ...
 $ maxT2 : int [1:88, 1:56] 1 1 1 1 1 1 1 1 1 1 ...
 $ minT2 : int [1:88, 1:56] 1 1 1 1 1 1 1 1 1 1 ...
 $ meanT2: int [1:88, 1:56] 1 1 1 1 1 1 1 1 1 1 ...

lapply() is the function to use and the "trick" is to use "TRUE" as the first argument to get all of the rows followed by negative indexing to remove the columns: lapply()是要使用的函数,“技巧”是使用“TRUE”作为第一个参数来获取所有行,然后使用负索引来删除列:

smallerMtx <- lapply(MatrixList, "[", TRUE, -c(17:40 , 49:56))
str(smallerMtx )
#------------
List of 8
 $ maxT  : int [1:120, 1:24] 1 1 1 1 1 1 1 1 1 1 ...
 $ minT  : int [1:120, 1:24] 1 1 1 1 1 1 1 1 1 1 ...
 $ meanT : int [1:120, 1:24] 1 1 1 1 1 1 1 1 1 1 ...
 $ rain24: int [1:120, 1:24] 1 1 1 1 1 1 1 1 1 1 ...
 $ rain5d: int [1:120, 1:24] 1 1 1 1 1 1 1 1 1 1 ...
 $ maxT2 : int [1:120, 1:24] 1 1 1 1 1 1 1 1 1 1 ...
 $ minT2 : int [1:120, 1:24] 1 1 1 1 1 1 1 1 1 1 ...
 $ meanT2: int [1:120, 1:24] 1 1 1 1 1 1 1 1 1 1 ...

I guess we need to resolve whether you really want rows or columns remove. 我想我们需要解决你是否真的要删除行或列。 (You said columns in the first sentence but later seemed to say rows. The numeric parts of your example/request suggested columns to be removed, but perhaps you come from a programming tradition where columns are specified first???? (你在第一句中说了一些列,但后来似乎在说行。你的示例/请求的数字部分建议删除列,但也许你来自编程传统,其中首先指定列????

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在R矩阵列表中的每个矩阵中添加两列? - How to add two columns from each matrix in a list of matrices in R? 从矩阵列表中删除矩阵 - remove matrix from a list of matrices 如何从R中的矩阵列表中获得均值矩阵 - How to obtain the mean-matrix from a list of matrices in R 从矩阵列表中获取选定的矩阵列 - Getting selected matrix columns from a list of matrices R中的矩阵矩阵(作为列表) - matrix of matrices (as a list) in R 在R中,有了矩阵列表,如何快速找到列表中每个矩阵的第一行和第二行之间的差异? - In R, with a list of matrices, how can I quickly find the difference between the first and second row in each matrix in the list? 根据每个矩阵上函数的结果从列表中删除单个矩阵-在r中 - Delete individual matrices from a list dependent upon the result of a function on each matrix - in r 如何将矩阵从矩阵列表转换为未列出的二维数字/矩阵而不展平为 R 中的向量? - How to convert a matrix from a list of matrices to an unlisted 2D numeric/matrix without flattening to a vector in R? R:如何从列表的每个内部元素中删除第一个元素而不将其转换为矩阵? - R: How do I remove the first element from each inner element of a list without converting it to matrix? 将多个等维矩阵的单元格值组合成一个矩阵,每个单元格是一个列表,作为 R 中输入矩阵的值 - Combining cell values of multiple equal dimension matrices to a single matrix with each cell is a list as values of the input matrices in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM