samedi 25 avril 2020

R if else code for combining two variables into one

I have two variables - Small_Vehicle and Large_Vehicle which are coded as 0 and 1s. I want to combine them into one variable where if the vehicle was small - 1, and if it was large - 2. If there is no data, I want R to leave is as NA. I have written the code below but it gives me the error:

Error: unexpected '}' in " TRIdata$Combined <- NA }"

The Combined variable already exists in the dataset but has the wrong values currently.

if (TRIdata$Small_Vehicle = 1) {
TRIdata$Combined<-1 } else {
if (TRIdata$Large_Vehicle=1)  {
TRIdata$Combined<-2 } else {
TRIdata$Combined <- NA }}

Do you have any suggestions about what I am doing wrong, please?

Thank you!

Aucun commentaire:

Enregistrer un commentaire