如何以更智能的方式从矩阵中删除特定的行和列?

时间:2021-06-22 04:39:07

Let's say t1 is :

假设t1是:

t1 <- array(1:20, dim=c(10,10))

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1   11    1   11    1   11    1   11    1    11
 [2,]    2   12    2   12    2   12    2   12    2    12
 [3,]    3   13    3   13    3   13    3   13    3    13
 [4,]    4   14    4   14    4   14    4   14    4    14
 [5,]    5   15    5   15    5   15    5   15    5    15
 [6,]    6   16    6   16    6   16    6   16    6    16
 [7,]    7   17    7   17    7   17    7   17    7    17
 [8,]    8   18    8   18    8   18    8   18    8    18
 [9,]    9   19    9   19    9   19    9   19    9    19
[10,]   10   20   10   20   10   20   10   20   10    20

I want to delete row 4-6 and column 7-9 from this matrix.

我想从这个矩阵中删除第4-6行和第7-9列。

I know how to remove it one by one using

我知道如何一个一个地去掉它

t2 <- t1[,-7]
t3 <- t2[,-8]
t4 <- t3[,-9]
t5 <- t4[-4,]
t6 <- t5[-5,]
t7 <- t6[-6,]

However, I believe it is the most stupid way of doing it. Could you mind to advice some smarter ways of doing it?

然而,我认为这是最愚蠢的做法。你能建议一些更聪明的方法吗?

3 个解决方案

#1


50  

You can do:

你能做什么:

t1<- t1[-4:-6,-7:-9]

#2


6  

You can use

您可以使用

t1<- t1[-4:-6,-7:-9]  

or

t1 <- t1[-(4:6), -(7:9)]

or

t1 <- t1[-c(4, 5, 6), -c(7, 8, 9)]

You can pass vectors to select rows/columns to be deleted. First two methods are useful if you are trying to delete continuous rows/columns. Third method is useful if You are trying to delete discrete rows/columns.

您可以传递向量来选择要删除的行/列。如果您试图删除连续行/列,那么前两种方法非常有用。第三种方法在尝试删除离散行/列时很有用。

> t1 <- array(1:20, dim=c(10,10));

> t1[-c(1, 4, 6, 7, 9), -c(2, 3, 8, 9)]

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    2   12    2   12    2   12
[2,]    3   13    3   13    3   13
[3,]    5   15    5   15    5   15
[4,]    8   18    8   18    8   18
[5,]   10   20   10   20   10   20

#3


1  

> S = matrix(c(1,2,3,4,5,2,1,2,3,4,3,2,1,2,3,4,3,2,1,2,5,4,3,2,1),ncol = 5,byrow = TRUE);S
[,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    2    1    2    3    4
[3,]    3    2    1    2    3
[4,]    4    3    2    1    2
[5,]    5    4    3    2    1
> S<-S[,-2]
> S
[,1] [,2] [,3] [,4]
[1,]    1    3    4    5
[2,]    2    2    3    4
[3,]    3    1    2    3
[4,]    4    2    1    2
[5,]    5    3    2    1

``

' '

Just Using the comand S <- S[,2] it removes the second column . Similarly to delete a row to delete second row S <- S[-2,]

只需使用comand S <- S[,2]就可以删除第二列。类似地,删除一行来删除第二行S <- S[-2,]

#1


50  

You can do:

你能做什么:

t1<- t1[-4:-6,-7:-9]

#2


6  

You can use

您可以使用

t1<- t1[-4:-6,-7:-9]  

or

t1 <- t1[-(4:6), -(7:9)]

or

t1 <- t1[-c(4, 5, 6), -c(7, 8, 9)]

You can pass vectors to select rows/columns to be deleted. First two methods are useful if you are trying to delete continuous rows/columns. Third method is useful if You are trying to delete discrete rows/columns.

您可以传递向量来选择要删除的行/列。如果您试图删除连续行/列,那么前两种方法非常有用。第三种方法在尝试删除离散行/列时很有用。

> t1 <- array(1:20, dim=c(10,10));

> t1[-c(1, 4, 6, 7, 9), -c(2, 3, 8, 9)]

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    2   12    2   12    2   12
[2,]    3   13    3   13    3   13
[3,]    5   15    5   15    5   15
[4,]    8   18    8   18    8   18
[5,]   10   20   10   20   10   20

#3


1  

> S = matrix(c(1,2,3,4,5,2,1,2,3,4,3,2,1,2,3,4,3,2,1,2,5,4,3,2,1),ncol = 5,byrow = TRUE);S
[,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    2    1    2    3    4
[3,]    3    2    1    2    3
[4,]    4    3    2    1    2
[5,]    5    4    3    2    1
> S<-S[,-2]
> S
[,1] [,2] [,3] [,4]
[1,]    1    3    4    5
[2,]    2    2    3    4
[3,]    3    1    2    3
[4,]    4    2    1    2
[5,]    5    3    2    1

``

' '

Just Using the comand S <- S[,2] it removes the second column . Similarly to delete a row to delete second row S <- S[-2,]

只需使用comand S <- S[,2]就可以删除第二列。类似地,删除一行来删除第二行S <- S[-2,]