我有以下说明
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
))
仅当f_008_x_...
的x = 1,2,3时,变量f_008_0 == x
才具有不丢失的数据。
它对我有用,但是我的问题是,是否存在一种主要使用dplyr
之类的vars, select, filter_at, summarized_at, mutate_at, any_vars, all_vars, if_else
函数的更优雅,更概括的方法来实现相同的目的。
我已尽一切努力,但我得到的最大是错误Error in UseMethod("tbl_vars") :
no applicable method for 'tbl_vars' applied to an object of class "quosures"