[問題] 多個xlsx併檔問題

看板R_Language作者 (日子過的真快阿)時間1年前 (2022/05/25 02:46), 1年前編輯推噓5(508)
留言13則, 3人參與, 1年前最新討論串1/1
各位好, 有併檔問題想要請較@@ 我有16個資料筆數很多的xlsx檔 想利用R併成一個大檔 首先就是多次讀入這16個檔,接著再把這16個檔合併 Code如下: library(openxlsx) library(dplyr) path <- "C:/Users/xxxx/Documents/R_" files <- list.files(path, pattern = "*.xlsx$", full.names = TRUE) dat <- list() for(i in 1:length(files)){ dat <- c(dat, list(read.xlsx(files[[i]], colNames = TRUE))) } DF <- rbind(dat[[1]], dat[[2]],........dat[[16]]) 但是覺得這樣好像很繁瑣... 網路上爬文發現有使用map_dfr 嘗試一下 files <- list.files("C:/Users/xxxx/Documents/R", pattern = "*.xlsx$", full.names = TRUE) DF <- map_dfr(files, read.xlsx) 但會跑出以下錯誤訊息: Error in `dplyr::bind_rows()`: ! Can't combine `..1$7.計畫日` <double> and `..2$7.計畫日` <character>. Run `rlang::last_error()` to see where the error occurred. 百思不得其解這著錯誤原因...是否代表我要將這16個檔的計劃日格式都弄成一樣... -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 180.176.212.49 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1653418010.A.66D.html ※ 編輯: kakaman (111.71.46.57 臺灣), 05/25/2022 02:53:10 ※ 編輯: kakaman (111.71.46.57 臺灣), 05/25/2022 02:54:25 ※ 編輯: kakaman (111.71.46.57 臺灣), 05/25/2022 02:55:40

05/25 03:53, 1年前 , 1F
第一個方法可以嗎?
05/25 03:53, 1F

05/25 03:54, 1年前 , 2F
如果可以
05/25 03:54, 2F

05/25 03:54, 1年前 , 3F
DF <- do.call(rbind, dat)
05/25 03:54, 3F

05/25 03:59, 1年前 , 4F
dat <- lapply(files, function(url)
05/25 03:59, 4F

05/25 03:59, 1年前 , 5F
read.xlsx (url, colNames = T))
05/25 03:59, 5F

05/25 04:01, 1年前 , 6F
然後報錯的原因 就 有的檔案是 char type 有的是 double
05/25 04:01, 6F

05/25 04:01, 1年前 , 7F
type
05/25 04:01, 7F

05/25 04:03, 1年前 , 8F
在計劃日那個column
05/25 04:03, 8F

05/25 04:05, 1年前 , 9F
第二個方法會錯 可能是因為 套件試圖解決原生dataframe
05/25 04:05, 9F

05/25 04:05, 1年前 , 10F
太慢 所以都會把column 的type 固定增加速度的樣子
05/25 04:05, 10F

05/25 19:14, 1年前 , 11F
05/25 19:14, 11F

05/25 22:49, 1年前 , 12F
另一個用map跟readxl的方法。readxl可以指定欄位格式
05/25 22:49, 12F

05/25 22:49, 1年前 , 13F
文章代碼(AID): #1YZIWQPj (R_Language)
文章代碼(AID): #1YZIWQPj (R_Language)