[R] How to manipulate the pointer of a file?

Greg Snow Greg.Snow at intermountainmail.org
Thu Apr 12 18:15:19 CEST 2007


?seek

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at intermountainmail.org
(801) 408-8111
 
 

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Yuchen Luo
> Sent: Thursday, April 12, 2007 9:33 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] How to manipulate the pointer of a file?
> 
> Dear friends.
> 
> With file( ) to obtain a pointer of a file,  every time we 
> use scan ( ) to read one row of it, the pointer will point to 
> the next row of the file. In the following example, d1 and d2 
> are obtained the same way but they correspond to different 
> rows of the same file because the pointer of the file moves 
> down a row when a row of the file is read.
> 
>  The following is an example:
> 
>  a1 <- list(name="Fred", wife="Mary", no.children=3)
> 
> a2 <- list(name="Tom", wife="Joy", no.children=9)
> 
> a3 <- list(name="Paul", wife="Alic", no.children=5)
> 
> 
> write.table(a1, file = "tt.csv", 
> sep=',',row.names=FALSE,col.name=TRUE)
> 
> write.table(a2, file="tt.csv", sep=',', append=TRUE, row.names=FALSE,
> col.names=FALSE)
> 
> write.table(a3, file="tt.csv", sep=',', append=TRUE, row.names=FALSE,
> col.names=FALSE)
> 
> 
> fp=file("tt.csv","r")
> 
>  c=scan(file=fp, sep=',', what=list(c1="", c2="", c3=""), flush=TRUE,
> nlines=1)
> 
>  d1=scan(file=fp, sep=',', what=list(name="", wife="", 
> no.kids=0), flush=TRUE, nlines=1)
> 
> d1
> 
> 
> R output:
> 
> -----------------------
> 
> $name
> 
> [1] "Fred"
> 
> 
> 
> $wife
> 
> [1] "Mary"
> 
> 
> 
> $no.kids
> 
> [1] 3
> 
> -------------------------
> 
>  d2=scan(file=fp, sep=',', what=list(name="", wife="", 
> no.kids=0), flush=TRUE, nlines=1)
> d2
> 
> 
> 
> R Output:
> 
> ------------
> 
> $name
> 
> [1] "Tom"
> 
> 
> 
> $wife
> 
> [1] "Joy"
> 
> 
> 
> $no.kids
> 
> [1] 9
> 
> ----------------------
> 
> My question is, how to manipulate the pointer of the file 
> further? For example, what if I need the pointer to go back 
> to the previous row?
> 
> 
> Best Wishes
> 
> Yuchen Luo
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list