How to check data frame if rowname matches with a vector of strings

I got one for you guys tonight!!!

I have a 30,000 x 30,000 data frame. I have a list of strings which may, or may not match some of the rownames in the df.

I need to test the list of strings I have to find out whether or not they are in the df or not. Here is a small reprex example:

df <- data.frame(
  one = c(2,1,2,0,0,1),
  two = c(4,5,3,0,1,3),
  three = c(1,0,2,0,7,4),
  four = c(3,2,1,0,0,0)
)

row.names(df) <- c('mm1','mm2','mm3', 'GC1', 'GC2', 'GC3')
df

    one two three four
mm1   2   4     1    3
mm2   1   5     0    2
mm3   2   3     2    1
GC1   0   0     0    0
GC2   0   1     7    0
GC3   1   3     4    0

rowname.list <- c("mm2", "mm3", "mm7", "GC2", "GC8")

Ideally I would like some kind of output where I can easily look at the list of rownames and see if there is a row with that name in the matrix. Something like this maybe:

    y
mm2 1
mm3 1
mm7 0
GC2 1
GC8 0

But anything would work. Thanks in advance I owe you guys alot!

You can use the %in% operator as shown below.

df <- data.frame(
  one = c(2,1,2,0,0,1),
  two = c(4,5,3,0,1,3),
  three = c(1,0,2,0,7,4),
  four = c(3,2,1,0,0,0)
)

row.names(df) <- c('mm1','mm2','mm3', 'GC1', 'GC2', 'GC3')
df
#>     one two three four
#> mm1   2   4     1    3
#> mm2   1   5     0    2
#> mm3   2   3     2    1
#> GC1   0   0     0    0
#> GC2   0   1     7    0
#> GC3   1   3     4    0

rowname.list <- c("mm2", "mm3", "mm7", "GC2", "GC8")
rowname.list %in% rownames(df)
#> [1]  TRUE  TRUE FALSE  TRUE FALSE

#or, in a data frame
DFnames <- data.frame(Names = rowname.list, Present = rowname.list %in% rownames(df))
DFnames
#>   Names Present
#> 1   mm2    TRUE
#> 2   mm3    TRUE
#> 3   mm7   FALSE
#> 4   GC2    TRUE
#> 5   GC8   FALSE

Created on 2019-11-23 by the reprex package (v0.3.0.9000)

1 Like

Thank you so much!! It worked perfectly

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.