读取多个 .txt 文件和 R 中的标头 [英] Read in multiple .txt files with header in R

查看:26
本文介绍了读取多个 .txt 文件和 R 中的标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在尝试使用 这个方法将我的数据导入R,但我不断收到错误:

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. 读取 .txt 文件
  2. 将标题与内容相关联
  3. 读取多个文件.

谢谢!!!

推荐答案

您尝试读取的文件之一似乎与标题具有相同的列数.要读取此文件,您可能必须更改此文件的标题,或使用更合适的列分隔符.要查看导致问题的文件,请尝试以下操作:

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

如果你不能让 read.table 工作,你总是可以依靠 readLines 并手动提取文件内容(例如,使用 strsplit).

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

这篇关于读取多个 .txt 文件和 R 中的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆