Michael
Michael

Reputation: 81

Error with ifelse statement when attempting to conditionally apply values

I have the following dataframe:

a <- c("OK", "OK", "2", "3", "5")
b <- c(1, 2, 3, 4, 5)
df <- data.frame(a, b)

If the value in df$a is "OK", I want the value to be whatever is in df$b.

If the value in df$a is anything other than "OK", I want it to remain.

So, ideally in this case, df$a would become: 1, 2, 2, 3, 5

I tried using the following ifelse statement, but get the error "the condition has length > 1 and only the first element will be used".

if(df$a == "OK") {
    df$a <- df$b
} else {
    df$a <- df$a
}

As a result, df$a just becomes df$b and the else statement isn't evaluated. I'm sure it's a simple fix, but not sure where I'm going wrong. Thanks for any help!

Upvotes: 1

Views: 39

Answers (1)

joel.wilson
joel.wilson

Reputation: 8413

actually the internally stored integers were being replaced . (levels of a factor variable are stored as integers internally)

> str(df)
'data.frame':   5 obs. of  2 variables:
 $ a: Factor w/ 4 levels "2","3","5","OK": 4 4 1 2 3
 $ b: num  1 2 3 4 5 

# convert the factor to character first   
df$a <- as.character(df$a)
ifelse(df$a == "OK", df$b, df$a) 
# OR
ifelse(df$a == "OK", df$b, as.character(df$a)) 

Upvotes: 1

Related Questions