Skip to content

Instantly share code, notes, and snippets.

@ClaytonJY
Last active March 10, 2019 17:26
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 ClaytonJY/b552320d5c73542a1e715798624d89ba to your computer and use it in GitHub Desktop.
Save ClaytonJY/b552320d5c73542a1e715798624d89ba to your computer and use it in GitHub Desktop.
Add column in base R, imputing with zeros
df <- data.frame(
id = 1:5,
label = LETTERS[1:5], # A, B, C, D, E
gen_1 = c(1.0, 1.1, NA, 1.3, NA),
gen_2 = c(2.0, NA, 2.2, 2.3, NA),
gen_3 = c(3.1, NA, 3.2, NA, NA)
)
# take a look
df
#> id label gen_1 gen_2 gen_3
#> 1 1 A 1.0 2.0 3.1
#> 2 2 B 1.1 NA NA
#> 3 3 C NA 2.2 3.2
#> 4 4 D 1.3 2.3 NA
#> 5 5 E NA NA NA
# replace NAs with 0 before summing
impute_and_sum <- function(x, y, z) {
x <- ifelse(is.na(x), 0, x)
y <- ifelse(is.na(y), 0, y)
z <- ifelse(is.na(z), 0, z)
x + y + z
}
# test it out
impute_and_sum(df$gen_1, df$gen_2, df$gen_3)
#> [1] 6.1 1.1 5.4 3.6 0.0
# put it in the df
df$gen_total <- impute_and_sum(df$gen_1, df$gen_2, df$gen_3)
# tada!
df
#> id label gen_1 gen_2 gen_3 gen_total
#> 1 1 A 1.0 2.0 3.1 6.1
#> 2 2 B 1.1 NA NA 1.1
#> 3 3 C NA 2.2 3.2 5.4
#> 4 4 D 1.3 2.3 NA 3.6
#> 5 5 E NA NA NA 0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment