我將多個 excel 檔案與一對多作業表結合在一起。他們每個人都有不同的列。我只對將作業表與地址資訊結合起來感興趣。對于沒有地址資訊的作業表,我需要在生成的組合檔案中記下它..如果我有問題,一個作業表有蔬菜沒有地址,另一個有地址資訊..我正在使用下面的代碼將它們放在一起。在我開始作業后,我將清理標準化它們并將它們放在一起。
dir_path <- "C:/temp/ConsigneeList/stuff4/" # target directory where the xlsx files are located.
re_file <- list.files(dir_path, pattern=".xls*") # regex pattern to match the file name format, in this case 'test1.xlsx', 'test2.xlsx' etc.
read_sheets <- function(dir_path, file){
xls_file <- paste0(dir_path, file)
xls_file %>%
excel_sheets() %>%
set_names() %>%
map_df(read_excel, path = xls_file, .id = 'sheet_name') %>%
mutate(file_name = file) %>%
select(file_name, sheet_name, everything())
}
number_of_excel_files<-length(file.list)
mybiggerlist<-vector('list',number_of_excel_files)
for(file in 1:length(mybiggerlist)) {
mybiggerlist[[file]]<-read_sheets(dir_path, file.list[file])
}
我收到錯誤:錯誤:無法組合Customer Quick REF$Order No 和CH Belt$Order No 。我嘗試使用 %>% mutate_all(as.character) 作為列本質上都應該是字符..關于如何解決這個問題的任何想法?或者,有沒有辦法跳過匯入有問題的資料并創建一行表明該作業表存在問題?謝謝!
uj5u.com熱心網友回復:
嘗試這樣的事情:
dir_path <- "C:/temp/ConsigneeList/stuff4/" # target directory where the xlsx files are located.
re_file <- list.files(dir_path, pattern=".xls*") # regex pattern to match the file name format, in this case 'test1.xlsx', 'test2.xlsx' etc.
read_sheets <- function(dir_path, file){
xls_file <- paste0(dir_path, file)
sheets <- xls_file %>%
excel_sheets() %>%
set_names() %>% ## not really sure if this is doing anything?
map(read_excel, path = xls_file)
# Now we have all the sheets in a list.
# Time to figure out which ones to combine
# Use purrr::keep to only keep sheets that meet some condition
# I just put in a wild guess, edit the test so that only sheets
# you want are kept
sheets <- purrr::keep(sheets, ~ "Address" %in% names(.))
bind_rows(sheets, .id = 'sheet_name') %>%
mutate(file_name = file) %>%
select(file_name, sheet_name, everything())
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/358136.html
下一篇:不使用回圈重新編碼多個變數
