Reading text files using read.table

后端 未结 1 1252
孤街浪徒
孤街浪徒 2021-02-01 06:19

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=\"         


        
1条回答
  •  说谎
    说谎 (楼主)
    2021-02-01 06:47

    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.

    0 讨论(0)
提交回复
热议问题