簡體   English   中英

r讀取多個.text文件和錯誤:列數多於列名

[英]r Reading multiple .text files and Error : more columns than column names

我正在嘗試根據以下腳本讀取將近30000個管道(|)分隔的文本文件

   mypath = "path/to/my/text/files/directory"
   setwd(mypath)

  # Create list of text files
  txt_files_ls = list.files(path=mypath, pattern="*.txt") 


  # Read the files in, assuming comma separator
  txt_files_df <- lapply(txt_files_ls, function(x) {read.table(file = x, header = T, sep ="|")})

  # Combine them
  Combined_df <- do.call("rbind", lapply(txt_files_df, as.data.frame))

我收到一個錯誤

Error in read.table(file = x, header = T, sep = "|") : 
  more columns than column names
In addition: There were 37 warnings (use warnings() to see them)

我的文件夾中有30000個文本文件,實際上無法打開每個文件並檢查哪個文件的列數超出了預期。

如果有人可以幫助我解決此錯誤,將很有幫助。 提前致謝。

最有可能的是,數據中的某些文本字段帶有| 其中,哪個read.table()令人困惑

嘗試使用功能更強大的data.table::fread()代替read.table()

library(data.table)

fread("filename.csv")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM