I'm trying to create a new column based on if there is a direct character match across 3 columns of the same row. I then want to put a 1 in the new column if there is a match, or a 0 if there is no match. Here's the code I have:
prodprov$var_match <- 0
for(i in 1:nrow(prodprov)) {
if(prodprov$var1[i] == prodprov$var2[i] |
prodprov$var1[i] == prodprov$var3[i] ) {
prodprov$var_match <- 1
}
}
But I get this error: Error in if (prodprov$var1[i] == prodprov$var2[i] | : missing value where TRUE/FALSE needed
Any ideas on how to fix this? All of these variables are the same data type (characters). Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire