简体   繁体   中英

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?

I think , you need

df$var1[df$var1 == "C"] <- ifelse(df[df$var1 == "C", "var2"] < 4, "B", "D")

This would still keep the level C in it.

df$var1
#[1] B B B D A D D D D A A A D B D B D D B D
#Levels: A B C D

You can drop the unused levels using droplevels

df$var1 <- droplevels(df$var1)

df$var1
#[1] B B B D A D D D D A A A D B D B D D B D
#Levels: A B D

Or just apply factor again

df$var1 <- factor(df$var1)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM