简体   繁体   中英

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

I have a problem when I use left_join() to join datasets and using lookup tables to further clean values. Any help with this problem would be extremely helpful since I am new to R. Thank you for your time.

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.

Below are my code

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')

It looks like your error happens here in the first step

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

The pipe structure is the same as this: lookup_replace(hpds_all_raw, 1, 'Departments and Agencies - EN')

And looking at your function lookup_replace(), that will left_join the first piece with the second piece.

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

So it seems hpds_all_raw might not have the column 'Departments and Agencies - EN'

Your code doesn't show us where hpds_all_raw comes from either

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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