mercredi 28 juin 2017

(R) How to compare string columns w/ logical operation (If (col1 == col2) {true} else {false}) [duplicate]

This question already has an answer here:

So i tried an if else statement but failed...

(I am working with string columns, in R)

hostpairs["hostspecificity"] <- NA

If (hostpairs$hostgenus2 == NA){
  hostpairs$hostspecificity == 0
} else if(hostpairs$hostgenus1 == hostpairs$hostgenus2){
  hostpairs$hostspecificity == 1
} else {
  hostpairs$hostspecificity == 2
}

Error in if (hostpairs$hostgenus2 == NA) { :
missing value where TRUE/FALSE needed
In addition: Warning message:
In if (hostpairs$hostgenus2 == NA) { : the condition has length > 1 and only the first element will be used

I am pretty sure there is a pretty dumb mistake somewhere in there...

Please help!

ps. Also if there is a function that checks two string column row by row and comes up with TRUE if they are equal (case insensitive, John = JOHN) or FALSE if they are not equal (John != Mary) That would be very helpful!

Thanks a lot

Aucun commentaire:

Enregistrer un commentaire