根据不同条件重新编码多列

时间:2018-12-23 17:05:12

标签: r dataframe

我需要根据不同的条件重新编码数据中的多列。我放了一个数据样本:

data_test <- structure(list(county = c("algarrobo", "alhue", "alto del carmen","ancud", "andacollo", "angol", "antofagasta", "yumbel", "yungay","zapallar"), 
year = c(2001, 2001, 2001, 2001, 2001, 2001, 2001,2016, 2016, 2016),             
ruling = c(0, 0, 0, 1, 0, 1, 1, 0, 0, 0),
adjacent_county1 = c("el quisco", "san pedro", "tierra amarilla","maullin", "coquimbo", "nacimiento", "mejillones", "cabrero","pemuco", "papudo"), 
adjacent_county2 = c("casablanca", "melipilla","vallenar", "dalcahue", "la serena", "renaico", "sierra gorda","quillon", "pinto", "la ligua"), 
adjacent_county3 = c(NA,"paine", "la higuera", "quemchi", "vicuna", "collipulli","san pedro de atacama", "florida", "antuco", "nogales"),
adjacent_county4 = c(NA, "mostazal", "vicuna", NA, "rio hurtado","ercilla", "taltal", "hualqui", "tucapel", "puchuncavi"),
adjacent_county5 = c(NA, "graneros", NA, NA, NA, "los sauces","diego de almagro", "san rosendo", "los angeles", NA), 
adjacent_county6 = c(NA,"rancagua", NA, NA, NA, "canete", NA, "laja", "cabrero",NA), 
adjacent_county7 = c(NA, "donihue", NA, NA, NA, "puren",NA, "los angeles", NA, NA), 
adjacent_county8 = c(NA, "coltauco",NA, NA, NA, "los alamos", NA, NA, NA, NA), 
adjacent_county9 = c(NA,"las cabras", NA, NA, NA, "curanilahue", NA, NA, NA, NA),
adjacent_county10 = c(NA_character_, NA_character_, NA_character_,NA_character_, NA_character_, NA_character_, NA_character_,NA_character_, NA_character_, NA_character_), 
adjacent_county11 = c(NA_character_,NA_character_, NA_character_, NA_character_, NA_character_,NA_character_, NA_character_, NA_character_, NA_character_,NA_character_)), 
row.names = c(NA, -10L), class = c("tbl_df","tbl", "data.frame"))

例如,我在2001年有Algarrobo个县,裁定=0。我需要将在下一列中出现的所有Algarrobo都重新编码为1,但仅限于2001年。数据框中的下一个县也是如此。我对这个问题非常复杂,因为我考虑了可能的解决方案,但是对于我必须做的所有可能的组合,它们都非常简单和成问题。例如:

data_test <- data_test %>% map_at(c("adjacent_county1", "adjacent_county2", 
  "adjacent_county3"),  function(x) {recode(x, "algarrobo" = "0")}) %>% bind_rows()

此外,我无法在这段代码中指定年份。

谢谢

0 个答案:

没有答案