rfsrc rfsrc - 1 year ago 78
R Question

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

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
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 Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download