I have the following instruction
df <- df %>%
mutate(cog = case_when(
f_008_0 == 1 & !(is.na(f_008_1_a) & is.na(f_008_1_b) & is.na(f_008_1_c) & is.na(f_008_1_d) & is.na(f_008_1_e) & is.na(f_008_1_f) & is.na(f_008_1_g) & is.na(f_008_1_h) & is.na(f_008_1_i) & is.na(f_008_1_j)) ~ rowSums(select(.,starts_with("f_008_1"),-ends_with("k"),-ends_with("l")), na.rm = T),
f_008_0 == 1 & f_008_1_l == 1 ~ 0,
f_008_0 == 1 & f_008_1_k == 1 ~ 997,
f_008_0 == 2 & !(is.na(f_008_2_a) & is.na(f_008_2_b) & is.na(f_008_2_c) & is.na(f_008_2_d) & is.na(f_008_2_e) & is.na(f_008_2_f) & is.na(f_008_2_g) & is.na(f_008_2_h) & is.na(f_008_2_i) & is.na(f_008_2_j)) ~ rowSums(select(.,starts_with("f_008_2"),-ends_with("k"),-ends_with("l")), na.rm = T),
f_008_0 == 2 & f_008_2_l == 1 ~ 0,
f_008_0 == 2 & f_008_2_k == 1 ~ 997,
f_008_0 == 3 & !(is.na(f_008_3_a) & is.na(f_008_3_b) & is.na(f_008_3_c) & is.na(f_008_3_d) & is.na(f_008_3_e) & is.na(f_008_3_f) & is.na(f_008_3_g) & is.na(f_008_3_h) & is.na(f_008_3_i) & is.na(f_008_3_j)) ~ rowSums(select(.,starts_with("f_008_3"),-ends_with("k"),-ends_with("l")), na.rm = T),
f_008_0 == 3 & f_008_3_l == 1 ~ 0,
f_008_0 == 3 & f_008_3_k == 1 ~ 997,
f_007 == 2 ~ 997,
TRUE ~ 999
))
where the variables f_008_x_...
have non missing data only if f_008_0 == x
for x=1,2,3.
It works for my, but my question is if there is a more elegant and summarized way to do the same using mainly dplyr
functions like vars, select, filter_at, summarized_at, mutate_at, any_vars, all_vars, if_else
, etc.
I tried every way I could but most I got was an error Error in UseMethod("tbl_vars") : no applicable method for 'tbl_vars' applied to an object of class "quosures"
Aucun commentaire:
Enregistrer un commentaire