Search code examples
rdummy-variable

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.


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)