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

Christoph Buser buser at stat.math.ethz.ch
Wed Jan 19 09:20:32 CET 2005


Dear Tim

You can use

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, sep = " ")
> Read 2 items
a
> [1] 235 335
b <- scan(cn.x, n=2, sep = " ")
> Read 2 items
b
> [1] 535 735
c <- scan(cn.x, n=2, sep = " ")
> Read 2 items
c
> [1] 115 135
d <- scan(cn.x, n=1, sep = " ")
> Read 1 items
d
> [1] 175

Regards,

Christoph Buser

-- 
Christoph Buser <buser at stat.math.ethz.ch>
Seminar fuer Statistik, LEO C11
ETH (Federal Inst. Technology)	8092 Zurich	 SWITZERLAND
phone: x-41-1-632-5414		fax: 632-1228
http://stat.ethz.ch/~buser/


Tim Howard writes:
 > 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
 > 
 > ______________________________________________
 > R-help at stat.math.ethz.ch mailing list
 > https://stat.ethz.ch/mailman/listinfo/r-help
 > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list