Last active
August 29, 2015 14:20
-
-
Save vnijs/8c47f4810873c8738756 to your computer and use it in GitHub Desktop.
impute NAs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dat <- data.frame(x = rep(1:3,2), | |
y = c(3,1,NA,3,NA,6)) | |
dat_org <- dat | |
ind <- complete.cases(dat) | |
model <- lm(y ~ x, data = dat[ind,]) | |
dat$res <- NA | |
dat[ind,"res"] <- model$res | |
dat | |
# x y res | |
# 1 1 3 0.6363636 | |
# 2 2 1 -2.5454545 | |
# 3 3 NA NA | |
# 4 1 3 0.6363636 | |
# 5 2 NA NA | |
# 6 3 6 1.2727273 | |
dplyr::full_join(dat_org,dat) | |
# Joining by: c("x", "y") | |
# x y res | |
# 1 3 0.6363636 | |
# 1 3 0.6363636 | |
# 2 1 -2.5454545 | |
# 3 NA NA | |
# 1 3 0.6363636 | |
# 1 3 0.6363636 | |
# 2 NA NA | |
# 3 6 1.2727273 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment