xniwniw
xniwniw

Reputation: 77

Which rows contain the specified element, using R

Here's a list, each row represents an random event

      [,1]        [,2]    [,3]   
 [1,] "Zebra"     "bat"   "lion" 
 [2,] "crocodile" "lion"  "bat"  
 [3,] "dog"       "fish"  "snake"
 [4,] "bat"       "zebra" "snake"
 [5,] "dog"       "Bear"  "bat"  
 [6,] "dog"       "Zebra" "lion" 
 [7,] "Bear"      "dog"   "Zebra"
 [8,] "pig"       "dog"   "snake"
 [9,] "zebra"     "lion"  "fish" 
[10,] "Bear"      "dog"   "Zebra"

I'm trying to use %in% command to check which of the events have a dog in them.

Upvotes: 0

Views: 65

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 389255

To get the row index which has the word 'dog' in it you can use rowSums as -

which(rowSums(mat == 'dog') > 0)
#[1]  3  5  6  7  8 10

data

mat <- structure(c("Zebra", "crocodile", "dog", "bat", "dog", "dog", 
"Bear", "pig", "zebra", "Bear", "bat", "lion", "fish", "zebra", 
"Bear", "Zebra", "dog", "dog", "lion", "dog", "lion", "bat", 
"snake", "snake", "bat", "lion", "Zebra", "snake", "fish", "Zebra"
), .Dim = c(10L, 3L), .Dimnames = list(NULL, NULL))

Upvotes: 1

r2evans
r2evans

Reputation: 160942

For basic equality, use which(., arr.ind=TRUE):

m <- matrix(1:25, nr=5)
m
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    1    6   11   16   21
# [2,]    2    7   12   17   22
# [3,]    3    8   13   18   23
# [4,]    4    9   14   19   24
# [5,]    5   10   15   20   25

which(m == 5L, arr.ind = TRUE)
#      row col
# [1,]   5   1

If you need set membership with %in%, it doesn't work just to replace == with %in%, unfortunately,

which(m %in% c(5L, 9L, 12L), arr.ind=TRUE)
# [1]  5  9 12

this is because m == . returns a matrix but m %in% . returns a vector (I don't recall why atm):

m == 5L
#       [,1]  [,2]  [,3]  [,4]  [,5]
# [1,] FALSE FALSE FALSE FALSE FALSE
# [2,] FALSE FALSE FALSE FALSE FALSE
# [3,] FALSE FALSE FALSE FALSE FALSE
# [4,] FALSE FALSE FALSE FALSE FALSE
# [5,]  TRUE FALSE FALSE FALSE FALSE

m %in% c(5L,9L,12L)
#  [1] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [20] FALSE FALSE FALSE FALSE FALSE FALSE

We can get around this by forcing the return to be a matrix:

which(matrix(m %in% c(5L,9L,12L), nrow=nrow(m)), arr.ind = TRUE)
#      row col
# [1,]   5   1
# [2,]   4   2
# [3,]   2   3

Upvotes: 1

Related Questions