Search code examples
rmissing-data

How to remove NAs from categorical data?


My data goes like this:

survey <- data.frame("Death" = c(1, 1, 1, NA, 0,1,NA,0,1,0),
                     "recover" = c(0, 0, 0, NA, 1,NA,0,1,0,NA))

My desired output is to replace NAs with the same data from the similar column.

I have tried mutations, but the answer is not coming correctly.


Solution

  • In base R we could use complete.cases to get cases without NA

    survey_complete <- complete.cases(survey)
    survey[survey_complete,]
    

    Output:

    > survey[survey_complete,]
      Death recover
    1     1       0
    2     1       0
    3     1       0
    5     0       1
    8     0       1
    9     1       0