rfsrc rfsrc - 2 months ago 6
R Question

R: Recode a single factor of a variable using multiple conditions

I have a factor variable with 4 levels:

set.seed(1)
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?

Answer

How about this?

sub.df <- df[df$var1=="C",] # subset you are concerned
sub.df$var1 <- ifelse(sub.df$var2 < 4, "B", "D") # modify
df[df$var1=="C",] <- sub.df # update the subset