How to check if a character vector contains a string

Viewed 439

I'm very new to R, just got RSTudio last week, so this might be a dumb question but anyway, I think I'm getting contradictory statements about whether or not my string "rs2418691" is in my vector rsIDcolumn. When I use the %in% command it says no, but using the which command does give me a coordinate for it in the vector:

> "rs2418691" %in% rsIDcolumn
[1] FALSE
> which(rsIDcolumn == "rs2418691")
[1] 137853

Does anyone know what's going on please? Thank you!

2 Answers

I think you are refering to a dataframe column. If you have a dataframe called df, which has a column named rsIDcolumn you can check if a string is inside of it by doing:

"rs2418691" %in% df$rsIDcolumn  

Just summing up, what is in the comment from @Adamm:

x <- data.frame(a=c("b", "c"))

"c" %in% x
#[1] FALSE

which(x == "c")
#[1] 2
Related