且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-25 23:35:22

在通常情况下,@ DWin早早得到了一个很好的答案.这是我简单的主意更容易理解的一种选择.

As is often the case, @DWin gets in early with an excellent answer. Here is an alternative that my simple mind finds easier to comprehend.

您可以使用lapply遍历列表,然后使用[运算符进行标准子设置.

You can use lapply to traverse your list, and then standard subsetting using the [ operator.

我不喜欢使用[运算符作为函数(如@DWin所建议的那样),我更喜欢在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 (i.e. subset a single matrix):

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 ...