mardi 21 novembre 2017

ifelse statement error in r

I'm creating a new column (called Environment) in my dataset (called data2) based on another column called "Site" where "1", "2", and "4" correspond to stream sites, "3" and "5" correspond to swamp sites, and "8" corresponds to the river site. Here's my code:

data2$Environment<-as.factor(data2$Environment<-ifelse(data2$Site==c("1", "2", "4"), "Stream", ifelse(data2$Site==c("8"),"River" ,"Swamp")))

When going through the new Environment column, it attributes the wrong name to the wrong site. For example, instead of 8 corresponding to River, sometimes it corresponds to Stream. This happens for other factors as well for the different combinations. Why is this happening? Any help is greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire