Skip to content

File reading.

8 messages · Randall Skelton, Roger Bivand, Laurent Gautier +3 more

#
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:

            
Maybe try file(), open(), and readLines(), see Brian Ripley's introduction
to using connections in R-News number 1, pages 16-17.

Roger
#
Indeed it was the 'file' command that I was missing.  Passing this object
through to 'scan' does what I was looking for.
Read 1 records
Read 39 records

The 'scan' documentation doesn't really explain this very well...

|     scan(file = "", what = double(0), nmax = -1, n = -1, sep = "",
|          quote = if (sep=="\n") "" else "'\"", dec = ".",
|          skip = 0, nlines = 0, na.strings = "NA",
|          flush = FALSE, fill = FALSE, strip.white = FALSE, quiet = FALSE,
|          blank.lines.skip = TRUE, multi.line = TRUE)

Thanks!
Randall
On Wed, 17 Oct 2001, Roger Bivand wrote:

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

            
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
What is the easiest (not the fastest) way to find the general inverse of a
matrix in R?

Many thanks,
Randall

NB: Is there a searchable mailing list available?

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

            
If you mean the generalized inverse, ginv() in package MASS.  Otherwise,
pleae tell us what a `general inverse' is.
#
I use solve(x) to find the inverse of a matrix (don't know what a "general
inverse" is). By the way, what is better: solve(x), qr.solve(x) or ginv(x)?
ginv(x) seems to give results for matrices where solve and qr.solve return
an error:
[,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
Error in solve.default(x) : singular matrix `x' in solve
Error in qr.solve(x) : singular matrix `x' in solve
[,1]          [,2]       [,3]
[1,] -0.6388889 -5.555556e-02  0.5277778
[2,] -0.1666667  4.163336e-17  0.1666667
[3,]  0.3055556  5.555556e-02 -0.1944444

Regards,

Philippe Grosjean


...........]<(({?<...............<?}))><...............................
 ) ) ) ) )	 __               	 __
( ( ( ( ( 	|__)              	|  _
 ) ) ) ) )	|   hilippe       	|__)rosjean
( ( ( ( ( 	Marine Biol. Lab., ULB, Belgium
 ) ) ) ) )	                  	 __
( ( ( ( ( 	|\  /|            	|__)
 ) ) ) ) )	| \/ |ariculture &	|__)iostatistics
( ( ( ( (
 ) ) ) ) )	e-mail: phgrosje at ulb.ac.be or phgrosjean at sciviews.org
( ( ( ( ( 	SciViews project coordinator (http://www.sciviews.org)
 ) ) ) ) )      tel: 00-32-2-650.29.70 (lab), 00-32-2-673.31.33 (home)
( ( ( ( (
 ) ) ) ) )      "I'm 100% confident that p is between 0 and 1"
( ( ( ( (                                  L. Gonick & W. Smith (1993)
 ) ) ) ) )
.......................................................................


-----Message d'origine-----
De : owner-r-help at stat.math.ethz.ch
[mailto:owner-r-help at stat.math.ethz.ch]De la part de Prof Brian Ripley
Envoye : jeudi 18 octobre 2001 04:25
A : Randall Skelton
Cc : r-help at stat.math.ethz.ch
Objet : Re: [R] General Matrix Inverse
On Wed, 17 Oct 2001, Randall Skelton wrote:

            
If you mean the generalized inverse, ginv() in package MASS.  Otherwise,
pleae tell us what a `general inverse' is.


--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

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


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
if A is singular, A^-1 is not defined but a generalized inverse G is,
namely

G is generalized inverse of A <=>

A G A = A  (sometimes G is written as A^-)

G is not unique, but adding 3 conditions

- G A G = G

- t(G A) = G A

- t(A G) = A G 

makes G unique (Moore-Penrose-Inverse)  

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