lundi 11 juillet 2016

Using ifelse to create a new variable in R, but getting error message unexpected '='

I'm pretty new to R (normally a SAS user), so I imagine this is quite an easy problem to fix even though I can't seem to figure out what's going wrong. I want to use the values in one of the variables in my dataset to create a new variable which just has zero or one if a given condition in the first variable is true. I tried using ifelse to create this new variable, but no matter what I try, R gives me an error message saying I have an unexpected '=' in my code.

Here is a sample of the data in the variable I'm using to create the second variable:

head(hdd$az_id, n=20)

 [1]  196  196  196  194  194  194  194  194  194  194  195  195 2170
2170  195  195  1576  196  196  194

Here is the ifelse code I tried to write:

hdd$pch<-ifelse(hdd$az_id=1576,1,0)

And the error message:

Error: unexpected '=' in "hdd$pima_hosp<-ifelse(hdd$az_fac_id="

Sorry if this has been answered elsewhere, I searched around quite a bit and couldn't find a solution myself!

Aucun commentaire:

Enregistrer un commentaire