簡體   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