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.