选择具有相同行的行的扩展名在不同的列中

时间:2021-08-10 13:17:41

I found this while searching for a similar approach.

我在寻找类似的方法时发现了这一点。

Selecting rows with same result in different columns in R

在R中的不同列中选择具有相同结果的行

Is there a way to search within a range of columns? Playing off the example in the link, what if instead of catch[catch$tspp.name == catch$elasmo.name,], is it possible to do this? catch[catch$tspp.name == c[23:56],] where R would search for values within columns 23 to 56 that match the tspp value?

有没有办法在一系列列中搜索?在链接中播放示例,如果不是catch [catch $ tspp.name == catch $ elasmo.name,],是否可以执行此操作? catch [catch $ tspp.name == c [23:56],]其中R将搜索第23到56列中与tspp值匹配的值?

Thanks in advance and please let me know whether it's better to post an independent question on a topic related to a previous post or to insert a follow up question within the aforementioned post.

在此先感谢您,请告诉我是否最好发布与上一篇文章相关的主题的独立问题,或在上述帖子中插入后续问题。

1 个解决方案

#1


1  

Here's one way to do it. This finds rows of X where the first column appears in columns 2 through 9.

这是一种方法。这将查找第一列出现在第2列到第9列中的X行。

> set.seed(1)
> X<-matrix(sample(10,100,T),10)
> X
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    3    3   10    5    9    5   10    4    5     3
 [2,]    4    2    3    6    7    9    3    9    8     1
 [3,]    6    7    7    5    8    5    5    4    4     7
 [4,]   10    4    2    2    6    3    4    4    4     9
 [5,]    3    8    3    9    6    1    7    5    8     8
 [6,]    9    5    4    7    8    1    3    9    3     8
 [7,]   10    8    1    8    1    4    5    9    8     5
 [8,]    7   10    4    2    5    6    8    4    2     5
 [9,]    7    4    9    8    8    7    1    8    3     9
[10,]    1    8    4    5    7    5    9   10    2     7
> X[rowSums(X[,1]==X[,2:9])>0,]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    3    3   10    5    9    5   10    4    5     3
[2,]    3    8    3    9    6    1    7    5    8     8
[3,]    9    5    4    7    8    1    3    9    3     8
[4,]    7    4    9    8    8    7    1    8    3     9

#1


1  

Here's one way to do it. This finds rows of X where the first column appears in columns 2 through 9.

这是一种方法。这将查找第一列出现在第2列到第9列中的X行。

> set.seed(1)
> X<-matrix(sample(10,100,T),10)
> X
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    3    3   10    5    9    5   10    4    5     3
 [2,]    4    2    3    6    7    9    3    9    8     1
 [3,]    6    7    7    5    8    5    5    4    4     7
 [4,]   10    4    2    2    6    3    4    4    4     9
 [5,]    3    8    3    9    6    1    7    5    8     8
 [6,]    9    5    4    7    8    1    3    9    3     8
 [7,]   10    8    1    8    1    4    5    9    8     5
 [8,]    7   10    4    2    5    6    8    4    2     5
 [9,]    7    4    9    8    8    7    1    8    3     9
[10,]    1    8    4    5    7    5    9   10    2     7
> X[rowSums(X[,1]==X[,2:9])>0,]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    3    3   10    5    9    5   10    4    5     3
[2,]    3    8    3    9    6    1    7    5    8     8
[3,]    9    5    4    7    8    1    3    9    3     8
[4,]    7    4    9    8    8    7    1    8    3     9