繁体   English   中英

dplyr left_join 出现“连接列必须存在于数据中”错误

[英]"Join columns must be present in data" error with dplyr left_join

当我使用left_join()加入数据集并使用查找表进一步清理值时,我遇到了问题。 由于我是 R 的新手,因此对这个问题的任何帮助都会非常有帮助。 感谢您的时间。

Error: Join columns must be present in data. x Problem with `Departments and Agencies - EN`. Run `rlang::last_error()` to see where the error occurred.

下面是我的代码

lookup_replace <- function(data, sheet, column){
  lookup <- read_xlsx('~/GAC/file26f868d29ad.xlsx', sheet = sheet, na = c("NA"))

  temp <- data %>% 
  left_join(lookup, by = setNames(column, column)) %>%
  mutate(!!sym(column) := new_value) %>%
  select(-new_value)
  return(temp)
}

# something is wrong with the french columns
hpds_all_replaced <- hpds_all_raw %>% 
  lookup_replace(1, 'Departments and Agencies - EN') %>%
  lookup_replace(2, 'Ministères et agences - FR') %>% 
  lookup_replace(3, 'Reporting Level 1 - EN') %>%
  lookup_replace(4, 'Niveau de rapport 1 - FR') %>%
  lookup_replace(5, 'Reporting Level 2 - EN') %>%
  lookup_replace(6, 'Niveau de rapport 2 - FR') %>%
  lookup_replace(7, 'GAC-Branch-EN') %>%
  lookup_replace(8, 'AMC-Direction générale-FR') %>%
  lookup_replace(12, 'Action Areas-EN') %>%
  lookup_replace(13, "Champs d'action-FR") %>%
  lookup_replace(14, 'Sub-Action Areas-EN')
  lookup_replace(15, "Volets des champs d'action-FR")
dataset_names <- list(
  'Departments and Agencies - EN' = one,
  'Ministères et agences - FR' = two,
  'Reporting Level 1 - EN' = three,
  'Niveau de rapport 1 - FR' = four,
  'Reporting Level 2 - EN' = five,
  'Niveau de rapport 2 - FR' = six,
  'GAC-Branch-EN' = seven,
  'AMC-Direction générale-FR' = eight,
  'Recipient Organization - EN' = nine,
  'Organisation bénéficiaire - FR' = ten,
  "GAC's Gender Equality" = eleven,
  'Action Areas-EN' = twelve,
  "Champs d'action-FR" = thirteen,
  'Sub-Action Areas-EN', fourteen,
  "Volets des champs d'action-FR" = fifteen,
  'GAC-Section-EN' = sixteen,
  'GAC-Division-EN' = seventeen,
  'Org level 1 - EN' = eighteen,
  'Org niveau 1 - FR' = nineteen,
  'Org level 2 - EN' = twenty,
  'Org niveau 2 - FR' = twenty_one)

write.xlsx(dataset_names, file = '~/GAC', sheetName = 'lookup_table.xlsx')

看起来你的错误发生在第一步

hpds_all_raw %>% 
  lookup_replace(1, 'Departments and Agencies - EN')

pipe 结构与此相同:lookup_replace(hpds_all_raw, 1, 'Departments and Agencies - EN')

并查看您的 function lookup_replace(),这将 left_join 第一块和第二块。

left_join(hpds_all_raw , read_xlsx(sheet=1), by column = 'Departments and Agencies - EN'])

因此,似乎 hpds_all_raw 可能没有“部门和机构 - EN”列

您的代码也没有向我们显示 hpds_all_raw 的来源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM