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

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Jan 19 09:42:57 CET 2005


This is because scan() has a private pushback.
Either:

1) Read the file a whole line at a time: I cannot see why you need to do 
so here nor in your sketched application.

or

2) Use an explicit separator, e.g. " " in your example.

scan() is not designed to read parts of lines of a file,


On Tue, 18 Jan 2005, Tim Howard wrote:

> 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
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list