I have a factor variable with 4 levels:
d <- sample(1:4,20,replace=TRUE)
df <- factor(d,labels=c("A","B","C","D"))
I want to distribute "C" between "B" and "D" conditional on the values of another variable:
df <- as.data.frame(df)
names(df)[names(df)=="df"] <- "var1"
df$var2 <- rnorm(20,5,2)
I thought I could subset "C" while ifelse recodes based on some criteria; say
df$var1[df$var1=="C"] <- ifelse(df$var1=="C" & df$var2 < 4, "B", "D")
But R doesn't like it (probably for a good reason) and allocates all Cs to "D". I suspect the second conditional does not work as intended. It is also possible that there is a more efficient solution even if this works. Thoughts?
Aucun commentaire:
Enregistrer un commentaire