[R] File reading.

Randall Skelton rhskelto at atm.ox.ac.uk
Wed Oct 17 13:05:53 CEST 2001


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



More information about the R-help mailing list