Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replace NAs with zeros using ifelse

I am trying to replace NAs in my variable- called violence- with zeros and create a new dummy variable using ifelse. The violence variable has several categories and NAs. The category ignore should be coded 1 but other categories including NAs should be coded 0. I use the following code:

   df$new_variable<-ifelse(is.na(df$violence)=="ignore",1,0)

However, the code did not produce any results.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There is syntax issue in the code

is.na(df$violence) == "ignore"

will be comparing the logical column derived from is.na with "ignore", instead if the description is as stated in the OP’s post – The category ignore should be coded 1 but other categories including NAs should be coded 0., use

df$new_variable <- +(df$violence %in% "ignore")

Here, we check for values that are "ignore" with %in% which returns a logical vector – TRUE only for "ignore" and FALSE for all others including NA (== returns NA for NA values) and then convert to binary with + (TRUE -> 1 and FALSE -> 0)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading