You can use coalesce
from dplyr
:
library(dplyr)
df %>%
mutate(DV_1 = coalesce(DV1_A, DV1_B, DV1_C),
DV_2 = coalesce(DV2_A, DV2_B, DV2_C))
If you have a lot of DV
columns to combine, you might not want to type all the column names. In this case, you can first grep
the column names for each DV
, parse each name to symbols with rlang::syms
, then splice (!!!
) the symbols in coalesce
(Advice from @hadley):
library(rlang)
var_quo1 = syms(grep("DV1", names(df), value = TRUE))
var_quo2 = syms(grep("DV2", names(df), value = TRUE))
df %>%
mutate(DV_1 = coalesce(!!! var_quo1),
DV_2 = coalesce(!!! var_quo2))
If instead, you have a ton of DV
's, you might not even want to type all the coalesce
lines, in this case, you can create a function that outputs one DV
column given an input number and lapply
+ bind_col
all of them together:
DV_combine = function(num_DVs){
DV_name = sym(paste0("DV", num_DVs))
DV_syms = syms(grep(paste0("DV", num_DVs), names(df), value = TRUE))
df %>%
transmute(!!DV_name := coalesce(!!! DV_syms))
}
bind_cols(df, lapply(1:2, DV_combine))
Result:
ID DV1_A DV1_B DV1_C DV2_A DV2_B DV2_C FACT DV_1 DV_2
1 1 1 NA NA 3 NA NA A 1 3
2 2 NA 4 NA NA 3 NA B 4 3
3 3 NA NA 5 NA NA 5 C 5 5
Note:
This method will work for both numeric
and character
class columns, but not factor
's. One should first convert the factor
columns to character before using this method.
Data:
df = structure(list(ID = c(1, 2, 3), DV1_A = c(1, NA, NA), DV1_B = c(NA,
4, NA), DV1_C = c(NA, NA, 5), DV2_A = c(3, NA, NA), DV2_B = c(NA,
3, NA), DV2_C = c(NA, NA, 5), FACT = structure(1:3, .Label = c("A",
"B", "C"), class = "factor")), .Names = c("ID", "DV1_A", "DV1_B",
"DV1_C", "DV2_A", "DV2_B", "DV2_C", "FACT"), row.names = c(NA,
-3L), class = "data.frame")