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

Fill NA values with random numbers generated from a specific distribution in R

I want to fill NA values with random numbers generated from a specific distribution in R. I have seen some methods to impute NA values with mean, but haven’t seen imputing with some numbers generated from a specific distribution.
For example, I have a data df as follows:

df <- data.frame(id = c(1,1,1,2,2,NA,2,3,NA,5,5,NA,9,9))Obviously there are some NA values. I want to replace them by numbers generated from a uniform distribution. Let’s say I want to impute NA’s with numbers from Uniform(4,8).
Is there a easy way to do this?

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 :

Try this

df$id <- ifelse(is.na(df$id) , runif(1,4,8) , df$id)
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