lundi 2 juillet 2018

R: using ifelse function for non-numerical values

I have some trouble using the ifelse option. I want to use this function in order to do a multiple linear regression. I have a large dataset and most of the data are "NA" and some others are words. The idea is to have NA = 0 and if not NA then it should be equal to 1. So I've done :

dsp = ifelse (sp == "NA", 0, 1)

But when I print dsp : I can see that the non NA values are changed to 1 but the NA values are not changed to 0 and are still the same.

I've tried the is.na() function, then I get FALSE for NA values and TRUE for non-NA values. I've tried ifelse( dsp == "False", 0, 1) but I have the same result as dsp = ifelse (sp == "NA", 0, 1)

Any idea about what should I do?

Thanks for your help :)

Aucun commentaire:

Enregistrer un commentaire