Hi there,
I'm still learning R and while reading "R for Data Science" from Wickham & Grolemud and in one of the exercises I am trying to add a column with TRUE and FALSE if the value matches a character vector with what it should be looking for.
Exercise 15.3.1 for those interested, this isn't the full code I'm prepping the data for a plot.
I was able to do what I wanted but using something I'm not familiar with but made sense the %in% operator.
gss_cat %>%
filter(!rincome %in% c("Not applicable")) %>%
mutate(rincome = fct_recode(rincome,
"Less than $1000" = "Lt $1000"
)) %>%
mutate(rincome_na = rincome %in% c("Refused", "Don't know", "No answer"))
Despite being successful I had to do quite a bit of research, which is great since I learned something new, but I was wondering how to do the same thing with nested ifs which would have been my first intuition.
This was my unsuccessful approach, why isn't this as straight forward as I thought it would?
Any suggestions on how I should have approached something that seemed so simple?
gss_cat %>%
filter(rincome != "Not applicable") %>%
mutate(rincome = fct_recode(rincome, "Less than $1000" = "Lt $1000")) %>%
mutate(rincome_na = if_else(rincome == "Refused",TRUE,
if_else(rincome == "Don't know",TRUE,
if_else(rincome == "No answer",TRUE))))
PS: I did do some search in the forum and couldn't find something that would elucidate this.
Thank you for your time and assistance.
Best regards,
LF.