mercredi 28 juin 2017

Nested Ifelse statement (How to compare 2 string columns in R)

I know that there are many questions around nested ifelse statements, but I was advised to use specifically ifelse since I want to test whether the values of two string columns are equal (columns are names and the test needs to also be case insensitive). I am really sorry that I will add another one of these questions but I am exhausted....

This is my code (columns are characters, I am trying to check for similar string data between the two columns and fill another column based on that)

hostpairs$hostspecificity <- ifelse(is.na(hostpairs$hostgenus1)|is.na(hostpairs$hostfamily1)|is.na(hostpairs$hostorder1), NA,
                                ifelse(is.na(hostpairs$hostgenus2), 1,
                                        ifelse(hostpairs$hostgenus1==hostpairs$hostgenus2), 1, 
                                                ifelse(hostpairs$hostfamily1==hostpairs$hostfamily2), 2,  
                                                        ifelse(hostpairs$hostorder1==hostpairs$hostorder2), 3, 
                                                                4)))))

ERROR

    Error: unexpected ')' in:
"                                                            ifelse(hostpairs$hostorder1==hostpairs$hostorder2), 3, 
                                                                    4)))"

EXPECTED RESULT

1   #Arithmetic column in the data frame filled with values according to if statement
2 
2
1
4
3
.
.

I cannot for the life of me understand why R has a problem with )s, and cannot fix it even after reading many similar questions here

Please help

Aucun commentaire:

Enregistrer un commentaire