What about trying to use a database system and make queries? If you are at the end of a file, you will read elements of length 0...
Uwe Ligges 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 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. ______________________________________________ R-help@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.