0
html_tab2 <- "new country helllo AF"
html_tab3 <- "new country helllo Afghanistan"

a1 <- c("Afghanistan","Pakistan")
a2 <- c("AF","PK")
df <- data.frame(a1,a2)
df$a1 <- as.character(df$a1)
df$a2 <- as.character(df$a2)

grep(df[1,1],html_tab2 , ignore.case = FALSE)
  1. understandable wrong

grep(df[1,2],html_tab2 , ignore.case = FALSE)

  1. works

grep(df[1,1]|df[1,2],html_tab2 , ignore.case = FALSE)

  1. does not work

3 should work because df[1,2] works. I am not sure how to write this OR statement

oguz ismail
  • 1
  • 16
  • 47
  • 69
  • 1
    Try `paste0(df[1,1], "|", df[1,2])` instead of `df[1,1]|df[1,2]` – Wiktor Stribiżew Mar 10 '20 at 15:01
  • 1
    The `|` operator in R is purely `logical`, meaning the element-wise "OR" between them. This is generally only meaningful when the elements are themselves `logical` (though `numeric` conversion does occur); `character` does not (as you see). If you want to know if something exists in one of the two, perhaps `grepl(df[1,1],...) | grepl(df[1,2],...)`, which will return a logical if your pattern exists in either (or both) of the cells. (This is vectorizable, so `grepl(df[,1],...) | grepl(df[,2],...)` will return a vector as long as the number of rows in `df`.) – r2evans Mar 10 '20 at 15:11
  • Is there a question in this question? – Chris Ruehlemann Mar 10 '20 at 17:39
  • `grep` returns a vector of the indices in the vector `html_tab2`. Since there is only one value and it does not match, `grep` returns `integer(0)`. That value will not work with logical `|` and two `grep` functions. Use `grepl` if you just want to know if one or the other is true as suggested by @r2evans. If you want to be able to identify `integer(0)`, read this [Check if value == integer(0) in R ](https://stackoverflow.com/questions/42603479/check-if-value-integer0-in-r) – dcarlson Mar 10 '20 at 19:10

0 Answers0