If else statement refuses to execute

I created a dictionary of unigrams, bigrams, trigrams, and 4-grams. The dictionary looks like this:

word1                 word2     frequency
will                            5153
like                            5081
get                             4821
let                   know       627
right                 now        575
look                  like       559
next                  week       478
social                media      465
let_us                know       194
let_know              think      173
cinco_de              mayo       172
manufacturer_custom   built      171
custom_built          painted    171

Here is a function to look up words in the dictionary (named dat)

nxtword1<- function(word){dat %>% filter(word1 == word) %>% select(-word1)}

I tested this function, and it worked.

The purpose of the following code is to look up a text string in the dictionary. Messages will be printed to the console, on whether the phrase appears in the dictionary or not.

predict<-nxtword1("new_manufacturer_custom_built")
if (row.names(predict)>0) {print("match found")
} else {print("no match found")}
predict

I knew that the phrase "new_manufacturer_custom_built" did not appear in the dictionary. Apparently R does not like my conditional statement. I was hoping that the else statement would be executed. Instead an error message appears

Error in if (row.names(predict) > 0) { : argument is of length zero

> else {print("no match found")}

> predict}

Apparently R does not like my conditional statement. How to fix this?

Your else command is on a different line and that's the reason

It should have been exactly after the curly braces of if statement like

If (true){
Something
} else {
Something
}

Else should be on the same line

Thanks for the correction. I submitted the corrected code

predict<-nxtword1("new_manufacturer_custom_built")
if (row.names(predict)>0) {print("match found")
}  else {print("no match found")}
Predict

I got a different error message this time:
Error in if (row.names(predict) > 0) { : argument is of length zero

To try to figure out what was wrong, I typed
> predict and got on the console

[1] word2     frequency
<0 rows> (or 0-length row.names)

I wanted R to execute the else statement, but it's still not doing that.

It will be easier if you provided a reproducible example. I would just do it this way and get rid of nxtword1 function.

 ifelse(word %in% dat$word1, print("match found"), print("match not found"))

That should be able to do it. Again

it means your predict object doesn't have row names.
some data frame just doesn't have row names.
try nrow(predict) instead.

Here is some from the dictionary
word1 word2 frequency
let know 627
right now 575
look like 559
next week 478
social media 465
let_us know 194
let_know think 173
cinco_de mayo 172
manufacturer_custom built 171
custom_built painted 171

Thank you for your code. It worked:) I tried elaborating on it to get it to execute functions I had written before.
checkdic <-function(word) {ifelse(word %in% dat$word1, nxtword1(word), nxtword1(less1gram(word)))
}
When the word appeared in the dictionary, all was well

checkdic("social")
[[1]]
[1] " " "media" "networks" "experience" "economic" "order" "good" "buying"

But if the word did not appear in the dictionary, there was an error message

checkdic("dog_social")

[1] "match found"

[1] "match found"

Show Traceback

Rerun with Debug

Error in word1 == word :

comparison (1) is possible only for atomic and list types

Thanks so much for your attention. What's going on now?

You really do have to pay attention to data types in R. Dataframes, lists, vectors, logical etc.

row.names(predict) is a vector of strings (possibly with length 0), so how can you compare it to > 0? nrow(predict) is probably what you want, as mentioned above.

The double square bracket shows that the nxtword1 function returns a list, and the first element [[1]] is a vector of strings.

However the less1gram function fails for words that are not in dat$word1 (which is a vector). You need to fix it to use %in% like you did with nxtword1, I suspect.

You seem to have multiple post. here is something I put together based on my understanding of your problem.

dat<- data.frame(word1=c( "will", "like","get", "look", "next", "social",
                          "cinco_de","manufacturer_custom", "custom_built"), 
                 word2=c(" ", " ", " ", "like","week", "media", "mayo", "built", "painted"), 
                 frequency = c( 5153, 5081, 4821, 559, 478,465, 172,171,171 ) )

word_exist <- function(word, df){
  ifelse(word %in% df$word1, "match found", "match not found")
}

word_exist("dog_social", dat)

Thank you for your attention. After reading your example, I fixed my code. This time it worked!
following_word<- function(phrase){match <-nxtword1(phrase)
if(nrow(match)>0){print(match)
} else{(nxtword1(less1gram(phrase)))
}
}

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