[英]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.