[R] How to signal the end of the table?

Vladimir Eremeev wl2776 at gmail.com
Thu Aug 30 16:39:44 CEST 2007


For example,

fp<-file("abc.csv","r")
c.row<-scan(file=fp,sep=",",nlines=1) # "what" argument is omitted for
bevity as it doesn't matter
rows<-c.row

while(length(c.row)>0) {
   c.row<-scan(file=fp,sep=";",nlines=1); 
   rows<-rbind(rows,c.row)
}
close(fp)

If you want to read a file by parts, then you do something like the
following (untested)
But this looks like a C-style approach, not R-style

fp<-file("abc.csv","r")
lines.skip<-0
for(i in 1:nparts){
  c.row<-scan(file=fp,sep=",",nlines=1,skip=lines.skip) 
  lines.read<-1
  rows<-c.row

  while(length(c.row)>0 && lines.read<part.size) {
     c.row<-scan(file=fp,sep=";",nlines=1); # nlines can be also >1
     rows<-rbind(rows,c.row)
     lines.read<-lines.read+1  # if nlines above is >1 then "+1" must be
replaced with nlines value
  }
  lines.skip<-lines.read

# do operations with rows
}
close(fp)



Yuchen Luo wrote:
> 
> I am using a "for" loop to read a table row by row and I have to specify
> how
> many records are there in the table. I need to read row by row because the
> table is huge and the memory not large enough for the whole table.:
> 
> 
> 
> number.of.records=100
> 
> 
> 
> fp=file("abc.csv","r")
> 
> pos=seek(fp, rw="read")
> 
> for (i in 1:number.of.record){
> 
> current.row=scan(file=fp, sep=',', what=list(count=1, cusip6="", idate=1,
> spread=1.1, vol252=1.1, vol1000=1.1, st_debt=1.1, lt_debt=1.1 ,
> total_liab=
> 1.1, cr=1.1,   shrout=1.1, prc=1.1, mkt_cap=1.1, rtng=1.1, sec=1.1,
> cr3m=1.1,
> cr5y=1.1, ust3m=1.1,  ust5y=1.1), flush=TRUE, nlines=1,quiet=T)
> 
> ...
> 
> }
> 
> 
> 
> I need to know the number of records in the table and put it in the
> variable
> named "number.of.records". When I have a new table that I do not know how
> many records it has, I use excel to open the file to figure it out and put
> it in variable " number.of.records". I often have many tables to try and
> every one of them has thousands of records----it takes a lot of time and
> trouble to adjust the code every time I read a different table.
> 
> 
> 
> I am wondering if I can change the "for" loop to a "while" loop:
> 
> 
> 
> while (the end of the table has not been reached)
> 
> {
> 
> current.row=scan(file=fp, sep=',', what=list(count=1, cusip6="", idate=1,
> spread=1.1, vol252=1.1, vol1000=1.1, st_debt=1.1, lt_debt=1.1 ,
> total_liab=
> 1.1, cr=1.1,   shrout=1.1, prc=1.1, mkt_cap=1.1, rtng=1.1, sec=1.1,
> cr3m=1.1,
> cr5y=1.1, ust3m=1.1,  ust5y=1.1), flush=TRUE, nlines=1,quiet=T)
> 
> ...
> 
> }
> 
> 
> The problem is how to articulate "while (the end of the table has not been
> reached)", or equivalently, how to signal the end of the table?
> 
> Best Wishes
> Yuchen Luo
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-signal-the-end-of-the-table--tf4353030.html#a12408586
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list