I am trying to use dplyr to mutate (or mutate_if?) column2 of a dataframe based on the contents of column1.
ID TEST PREF
11 true blue
23 false red
4 false yellow
if test == "false", I would like to mutate PREF = "orange". Otherwise, do not change PREF.
ID TEST PREF
11 true blue
23 false orange
4 false orange
I thought an ifelse statement might work but can't understand how to make else do nothing. It seems to return the column number instead of the contents at that row.
df <- data.frame(ID = c("11", "23", "4"),
TEST = factor(c("true", "false", "false")),
PREF = factor(c("blue", "red", "yellow")))
df <- df %>%
mutate(PREF = ifelse(TEST == "false", "orange", PREF))
I feel like mutate_if should be appropriate but I don't think I understand its function very well and I can't find any examples similar to what I need. Something like:
df <- df %>%
mutate_if(TEST == "true", PREF = "orange")
Can anyone please give me some suggestions? Thanks!
Edit:
I have realised that in my ifelse statement it was returning the level of the factor, not the characters I wanted. This works exactly as I was hoping by specifying as.character.
df2 <- df %>%
mutate(PREF = factor(ifelse(TEST == "false", "white", as.character(PREF))))
test
toTEST
andpref
toPREF
. R is case-sensitive. – joshpkmutate_if
is not the right function. Theif
part of that command looks at properties of the column itself, not any particular row value. You would usemutate_if
to change all the numeric columns or characters or impute data in columns with missing values.ifelse
is a better choice (note sure what the problem is with the code you posted), see also the "tidy" version calledif_else
or even the base functionreplace()
might be a better choice. – MrFlick