Use mutate() to return a logical vector - R for Data Science

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.

I think your last if_else is missing the FALSE that should be returned if none of the previous conditions were TRUE. A simplified example is below. I think the %in% solution is preferable but use whatever works for you.

DF <- data.frame(rincome = c("A", "B", "C", "D", "E", "A", "Z"))
library(dplyr)

DF <- DF %>% mutate(income_na = if_else(rincome == "A", TRUE,
                                        if_else(rincome == "B", TRUE,
                                        if_else(rincome == "C", TRUE, 
                                        FALSE))))
DF
#>   rincome income_na
#> 1       A      TRUE
#> 2       B      TRUE
#> 3       C      TRUE
#> 4       D     FALSE
#> 5       E     FALSE
#> 6       A      TRUE
#> 7       Z     FALSE

Created on 2020-02-09 by the reprex package (v0.2.1)

1 Like

Thank you, I can't believe it was just a missing argument... I was looking at this for about 2h and working around it.

This is me right now: :man_facepalming:t2:

Word of advice, if you find that you are considering nested if else, it's always worth also first considering case_when which has readability advantages in my opinion

1 Like

Thank you I will look into it.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.