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 missing values in R dataframe

I have data:

Date Price
"2021-01-01" 1
"2021-01-02" NA
"2021-01-03" NA
"2021-01-04" NA
"2021-01-05" NA
"2021-01-06" 6
"2021-01-07" NA
"2021-01-08" NA
"2021-01-09" 3

And I would like to replace missing values with means, so that the end result would look like this:

Date Price
"2021-01-01" 1
"2021-01-02" 2
"2021-01-03" 3
"2021-01-04" 4
"2021-01-05" 5
"2021-01-06" 6
"2021-01-07" 5
"2021-01-08" 4
"2021-01-09" 3

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 :

You can use zoo::na.approx:

library(zoo)
na.approx(dat$Price)
# [1] 1 2 3 4 5 6 5 4 3
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