0

I'm trying to use an if statement to conditionally recode my NAs as 0s. The if statement has two conditions and is followed by a bracketed replace_na argument. I've played with it quite a bit and it keeps giving me the unexpected symbol error message.

From what I understand this error means that you're missing some sort of punctuation, so I've gone through and added and deleted parentheses and brackets, and commas and split the code on to multiple lines and I can't get anything to run.

My data are all numeric, but here's an example using the starwars dataset because it's actually built-in dataset with missing data.

starwars <- if(starwars$eye_color!=yellow & homeworld==Tatooine) {
              starwars$birth_year replace_NA(0)
            }

I expect that for rows where eye_color is not yellow and homeworld is Tatooine, birth_year will be recoded from NA to 0.

What I get is: Error:

unexpected symbol in "starwars<-if(starwars$eye_color!=yellow & homeworld==Tatooine){starwars$birth_year replace_NA"

I can't figure out what I'm missing or how I've got it set up wrong.

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
Carley
  • 63
  • 6
  • 2
    This is wrong in at least four ways. (1) `starwars$birth_year replace_NA(0)` is missing an operator, the space between the frame/column and the function-call makes no sense. (2) If you mean `tidyr::replace_na`, then your case is wrong. (3) You cannot use vector-comparisons in a singular `if` statement, you might want `ifelse` (or `if_else` if tidy). (4) I think @IceCreamToucan's comment is (err... was?) most likely apropos, inferring what you need. (5) Quotes around `yellow` and `Tatooine`? – r2evans Aug 19 '19 at 20:37
  • 2
    `starwars %>% mutate(birth_year = if_else(eye_color == "yellow" & homeworld == "Tatooine" & is.na(birth_year), 0, birth_year))`? – r2evans Aug 19 '19 at 20:38
  • Take a look at the docs for `tidyr::replace_na`: it takes a data frame, then a named list of replacements – camille Aug 19 '19 at 20:43
  • The above comment-code *should be*: `starwars %>% mutate(birth_year = if_else(eye_color != "yellow" & homeworld == "Tatooine" & is.na(birth_year), 0, birth_year))` (I had the yellow inequality wrong). – r2evans Aug 19 '19 at 20:44
  • I still think @IceCreamToucan's original suggested-link (since deleted) might be appropriate: https://stackoverflow.com/q/8161836 – r2evans Aug 19 '19 at 20:45
  • Sorry, deleted after I realized they weren't trying to replace all NAs. On reflection I guess it is still valuable. @carley you could also do this in Base R with `replace`: `starwars$birth_year <- with(starwars, replace(birth_year, eye_color != 'yellow' & homeworld != 'Tatooine' & is.na(birth_year), 0))` – IceCreamToucan Aug 19 '19 at 20:47
  • Thanks @IceCreamToucan that was simple and worked well! – Carley Aug 20 '19 at 14:48

0 Answers0