Read in multiple .txt files with header in R

倖福魔咒の 提交于 2019-12-13 02:56:39

问题


Okay, I'm trying to use this method to get my data into R, but I keep on getting the error:

  Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  line 1 did not have 22 elements

This is the script that I'm running:

library(foreign)

setwd("/Library/A_Intel/")

filelist <-list.files()

#assuming tab separated values with a header    
 datalist = lapply(filelist, function(xx)read.table(xx, header=T, sep=";")) 

#assuming the same header/columns for all files
 datafr = do.call("rbind", datalist) 

Keep in mind, my priorities are:

  1. To read from a .txt file
  2. To associate the headers with the content
  3. To read from multiple files.

Thanks!!!


回答1:


It appears that one of the files you are trying to read does have the same number of columns as the header. To read this file, you may have to alter the header of this file, or use a more appropriate column separator. To see which file is causing the problem, try something like:

datalist <- list()
for(filename in filelist){
  cat(filename,'\n')
  datalist[[filename]] <- read.table(filename, header = TRUE, sep = ';')
}

Another option is to get the contents of the file and the header separately:

datalist[[filename]] <- read.table(filename, header = FALSE, sep = ';')
thisHeader <- readLines(filename, n=1)
## ... separate columns of thisHeader ...
colnames(datalist[[filename]]) <- processedHeader

If you can't get read.table to work, you can always fall back on readLines and extract the file contents manually (using, for example, strsplit).




回答2:


To keep in the spirit of avoiding for loops an initial sanity check before loading all of the data could be done with

lapply(filelist, function(xx){
       print (scan(xx, what = 'character', sep=";", nlines = 1))} )

(assuming your header is separated with ';' which may not be the case)



来源:https://stackoverflow.com/questions/3439410/read-in-multiple-txt-files-with-header-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!