简体   繁体   中英

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

ie, 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?

This can be done as follows:

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

Thanks Richard!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM