Search code examples
ranalysis

Remove NAs from data frame


I have my data as follows:

Date         Med1    Med2    Med3    Med4
2013-03-01    19      23     NA       33
2013-03-01    25      NA     19       27
2013-03-01    26      23     15       NA
2013-03-01    NA      27     NA       25

I would want my data in the following format:

Date         Med1    Med2    Med3    Med4
2013-03-01    19      23              33
2013-03-01    25             19       27
2013-03-01    26      23     15        
2013-03-01            27              25

i.e, I want to replace the NAs with empty cells.

I tried functions such as na.omit(df), na.exclude(df). In both the cases, the row which has NA is being omitted or excluded. I dont want to drop off the entire row or column but just the NA.

Please note that I dont want the NAs to be replaced by 0s. I want a blank space replacing NA.

Is there a way to do that?


Solution

  • This can be done as follows:

    df[is.na(df)] <- ""
    

    Thanks Richard!