0

I want to check if all cells of a column of a dataframe contain a word of all cells of another dataframe.

I successfully checked if a certain cell of a dataframe contains a certain cell of another dataframe with the following code:

if(grep(geoplaces$name[1], adresses$Comments[1])){
  print("hello")
} else {
  print("error")
}

So now I want to loop that function for all rows of geoplaces$name and all rows of adresses$Comments:

So I added the following code:

 ig <- 1
 ia <- 1

 for(ia in 1:8){
     for(ig in 1:8){
         if(grep(geoplaces$name[ig], adresses$Comments[ia])){
             print("hello")
             ig <- ig + 1
         } else {
             print("error")
             ig <- ig + 1
         }
      }
      ia <- ia + 1
 }

However I'm receiving the following error:

Error in if (grep(geoplaces$name[ig], adresses$Comments[ia])) { : argument is of length zero.

Any suggestions?

cmaher
  • 5,100
  • 1
  • 22
  • 34
Stathis G.
  • 145
  • 2
  • 11
  • Check out this similar question: https://stackoverflow.com/questions/12307139/error-in-if-while-condition-argument-is-of-length-zero. Add an is.null() check on the values used in your if statement before applying the logical test. – icj Apr 06 '18 at 15:58

1 Answers1

2

The issue is that grep returns an index rather than a logical value. You want to use grepl which will return a logical value. See the grep documentation.

edit: A couple things can cause your followup error:

  • geoplaces and/or addresses contain less than 8 rows of data
  • geoplaces contains NA values that need to be handled before making the grepl comparison (R thinks the search pattern is undefined)

It's impossible to say without having the complete dataset. The base functions is.na and nrow can help deal with either possibility.

icj
  • 719
  • 6
  • 12
  • To add on to this: since `grep` returns the index of the match, when it cannot find a match, it returns a integer vector of length 0 (`integer(0)` in the console) which basically no functions (aside from `length`) know how to handle. – divibisan Apr 06 '18 at 16:56
  • Well actually, after using grep1 the error now has been transformed to: Error in if (grepl(geoplaces$name[ig], adresses$Comments[ia])) { : argument is not interpretable as logical – Stathis G. Apr 06 '18 at 17:15