Reading text files using read.table

烂漫一生 提交于 2019-12-03 05:47:20

问题


I have a text file with an id and name column, and I'm trying to read it into a data frame in R:

d = read.table("foobar.txt", sep="\t")

But for some reason, a lot of lines get merged -- e.g., in row 500 of my data frame, I'll see something like

row 500: 500 Bob\n501\tChris\n502\tGrace

[So if my original text file has, say, 5000 lines, the dimensions of my table will only end up being 1000 rows and 2 columns.]

I've had this happen to me quite a few times. Does anyone know what the problem is, or how to fix it?


回答1:


From ?read.table: The number of data columns is determined by looking at the first five lines of input (or the whole file if it has less than five lines), or from the length of col.names if it is specified and is longer. This could conceivably be wrong if fill or blank.lines.skip are true, so specify col.names if necessary.

So, perhaps your data file isn't clean. Being more specific will help the data import:

d = read.table("foobar.txt", 
               sep="\t", 
               col.names=c("id", "name"), 
               fill=FALSE, 
               strip.white=TRUE)

will specify exact columns and fill=FALSE will force a two column data frame.



来源:https://stackoverflow.com/questions/1407647/reading-text-files-using-read-table

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