Skip to content

Instantly share code, notes, and snippets.

@johnjosephhorton
Created June 15, 2012 00:34
Show Gist options
  • Save johnjosephhorton/2933874 to your computer and use it in GitHub Desktop.
Save johnjosephhorton/2933874 to your computer and use it in GitHub Desktop.
Dealing with missing values in R
n <- 100
x <- runif(n)
y <- 5*x
missing <- runif(n) > .80
df <- data.frame(y = y, x = x, missing = missing)
df$x[missing] <- mean(df$x[!missing])
df$y[!df$missing] <- df$y[!df$missing] + .80
m <- lm(y ~ x + missing, data = df)
summary(m)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment