Skip to content

Instantly share code, notes, and snippets.

@Keiku
Last active January 26, 2017 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Keiku/a9400d072a5d74d19000e0ab6845e43b to your computer and use it in GitHub Desktop.
Save Keiku/a9400d072a5d74d19000e0ab6845e43b to your computer and use it in GitHub Desktop.
impute a included NA valiable.
library(dplyr)
data <- data_frame(var = c(0, NA, 2))
data %>% mutate(var = coalesce(var, 1))
data %>% mutate(var = replace(var, which(is.na(var)), 1))
data %>% mutate(var = if_else(is.na(var), 1, var))
# A tibble: 3 × 1
# var
# <dbl>
# 1 0
# 2 1
# 3 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment