[R] Dropping a digit with scan() on a connection

Tim Howard tghoward at gw.dec.state.ny.us
Tue Jan 18 21:36:49 CET 2005


R gurus,

My use of scan() seems to be dropping the first digit of sequential
scans on a connection. It looks like it happens only within a line:

> cat("TITLE extra line", "235 335 535 735", "115 135 175",
file="ex.data", sep="\n")
> cn.x <- file("ex.data", open="r")
> a <- scan(cn.x, skip=1, n=2)
Read 2 items
> a
[1] 235 335
> b <- scan(cn.x, n=2)
Read 2 items
> b
[1]  35 735
> c <- scan(cn.x, n=2)
Read 2 items
> c
[1] 115 135
> d <- scan(cn.x, n=1)
Read 1 items
> d
[1] 75
> 

Note in b, I should get 535, not 35 as the first value. In d, I should
get 175.  Does anyone know how to get these digits?

The reason I'm not scanning the entire file at once is that my real
dataset is much larger than a Gig and I'll need to pull only portions of
the file in at once. I got readLines to work, but then I have to figure
out how to convert each entire line into a data.frame. Scan seems a lot
cleaner, with the exception of the funny character dropping issue.

Thanks so much!
Tim Howard




More information about the R-help mailing list