Hi all,
Appologies for the rather basic IO question but I am rather new to R...
Migrated from IDL/Matlab recently. I have a rather simple Fortran
control file (sigh...) that I am trying to parse and read using R. My
problem is that the file's format is somewhat flexible. Imagine:
---
1> 39 1901
2> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
3> 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58
4> 650.0 650.5 651.0 651.5 652.0 652.5 653.0 653.5 654.0
5> 654.5 655.0 655.5 656.0 656.5 657.0 657.5 658.0 658.5
...
7> 1599.5 1600.0
8>
9> 1 1 0
10> Xtemp.wf
11> 2
12> Xtemp.noise
13> 4
14> Xtemp.Sa
---
Line 1 contains 2 numbers (nx, ny) which can easily be read using scan.
If nx>0 then there are nx values to be read next (i.e. lines 2 and 3 have
39 values). Likewise, with ny>0, lines 4-7 give 1901 values. However, if
nx or ny are less than 0, then the data block for lines 2-3 or lines 4-7
will not be present and a generic index is made... In psudo code:
---
# get the file handle for the case file
fin=fopen(casefile);
# read the file
in=fscanf(fin,'%f',2);
# first 2 numbers in the case file give the size
nx=in(1);
ny=in(2);
# read or construct the x label
if nx>0
xlab=fscanf(fin,'%f',nx);
else
nx=-nx;
xlab=0:(nx-1);
end
# read or construct the y label
if ny>0
ylab=fscanf(fin,'%f',ny);
else
ny=-ny;
ylab=1:ny;
end
---
My problems are that R seems to be tied to the line numbers on which data
is found and that the scan function doesn't remember where it last was...
Is there some C-like fopen/fread construct that I am missing?
Cheers,
Randall
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Wed, 17 Oct 2001, Randall Skelton wrote:> Hi all, > > Appologies for the rather basic IO question but I am rather new to R... > Migrated from IDL/Matlab recently. I have a rather simple Fortran > control file (sigh...) that I am trying to parse and read using R. My > problem is that the file's format is somewhat flexible. Imagine: > > --- > 1> 39 1901 > 2> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 > 3> 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 > 4> 650.0 650.5 651.0 651.5 652.0 652.5 653.0 653.5 654.0 > 5> 654.5 655.0 655.5 656.0 656.5 657.0 657.5 658.0 658.5 > ... > 7> 1599.5 1600.0 > 8> > 9> 1 1 0 > 10> Xtemp.wf > 11> 2 > 12> Xtemp.noise > 13> 4 > 14> Xtemp.Sa > --- > > Line 1 contains 2 numbers (nx, ny) which can easily be read using scan. > If nx>0 then there are nx values to be read next (i.e. lines 2 and 3 have > 39 values). Likewise, with ny>0, lines 4-7 give 1901 values. However, if > nx or ny are less than 0, then the data block for lines 2-3 or lines 4-7 > will not be present and a generic index is made... In psudo code: > > --- > # get the file handle for the case file > fin=fopen(casefile); > > # read the file > in=fscanf(fin,'%f',2); > > # first 2 numbers in the case file give the size > nx=in(1); > ny=in(2); > > # read or construct the x label > if nx>0 > xlab=fscanf(fin,'%f',nx); > else > nx=-nx; > xlab=0:(nx-1); > end > > # read or construct the y label > if ny>0 > ylab=fscanf(fin,'%f',ny); > else > ny=-ny; > ylab=1:ny; > end > --- > > My problems are that R seems to be tied to the line numbers on which data > is found and that the scan function doesn't remember where it last was... > Is there some C-like fopen/fread construct that I am missing? >Maybe try file(), open(), and readLines(), see Brian Ripley's introduction to using connections in R-News number 1, pages 16-17. Roger -- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93 e-mail: Roger.Bivand at nhh.no and: Department of Geography and Regional Development, University of Gdansk, al. Mar. J. Pilsudskiego 46, PL-81 378 Gdynia, Poland. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Laurent Gautier CBS, Building 208, DTU PhD. Student D-2800 Lyngby,Denmark tel: +45 45 25 24 85 http://www.cbs.dtu.dk/laurent On Wed, 17 Oct 2001, Randall Skelton wrote:> My problems are that R seems to be tied to the line numbers on which data > is found and that the scan function doesn't remember where it last was... > Is there some C-like fopen/fread construct that I am missing?The use of connections could be something worth being tried. The command 'readLines' reads lines from a connection (and in this sense 'remembers' where it last was...) try help(file) help(readLines) Hopin' it helps, Laurent -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Apparently Analagous Threads
- Problem with factors in lm() when using Rscript but not GUI
- [LLVMdev] Reviving the new LLVM concurrency model
- [PATCH] libxl: When checking BDF of existing slots, function should be decimal, not hex
- [Bridge] two fields are missing in brctl output when using /sys
- Odd behavior of plot function