Skip to content

read.table() versus scan()

4 messages · H Roark, Tal Galili, Gabor Grothendieck +1 more

#
On Thu, Jan 27, 2011 at 11:23 PM, H Roark <hrbuilder at hotmail.com> wrote:
Read the header into nms and then the data into DF and then put them together:

con <- file("myfile.dat")
nms <- scan(con, what = "", nlines = 1)
DF <- read.table(con, fill = TRUE)
DF <- setNames(DF[seq_along(nms)], nms)

or just read it twice: first the one line of the header and then the data:

nms <- unlist(read.table("myfile.dat", nrows = 1))
DF <- read.table("myfile.dat", fill = TRUE, skip = 1)
DF <- setNames(DF[seq_along(nms)], nms)
#
On 2011-01-27 20:23, H Roark wrote:
Note this comment in the Details section of ?read.table:

    "The number of data columns is determined by looking
     at the first five lines of input ..."

Peter Ehlers