[R] R help for a newby

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Sun Nov 12 22:59:17 CET 2000


tmed <tmed at pacbell.net> writes:

> Hello,
> 
>     I'm trying to read a file that has ascci data in it. The format of =
> the data
> is in this order: test_number(integer) 4 double readings. Simple,ok, but =
> for=20
> life of me I can't get R to put it into the internal format I need. What =
> I want
> to do is to make an array called dat and dim it 10000 rows by 5 columns.
> That seems simple enough, I start out by:
> 
> dat <- array(0,dim=3Dc(10000,5)) this looks ok.=20
> The next thing I did was to scan in the data.
> dat <-scan("mydata-file",what=3Darray(""),sep=3D"\n").
> I get the data into the dat array but its looks like a string.
> ie "1000 34.5  34.7  35  35.6".
> what I want is to make each of these data point go into=20
> the appropriate array index ie:
> dat[1][1]   dat[1][2]  dat[1][3]   dat[1][4]   dat[1][5]
>     1000     34.5        34.7         35           35.6
>
> Any help would be greatly appreciated.

matrix(scan("mydata-file"), ncol=5, byrow=TRUE)

or

do.call("cbind", scan("mydata-file", what=rep(0,5)))

or

as.matrix(read.table("mydata-file"))
 
should all work ("should" being code for "I didn't actually try"). I'd
expect the middle one to be the most efficient, but who knows.

BTW: Accessing an element of a matrix is dat[1,1] in R, not dat[1][1]
as in C.

> ------=_NextPart_000_0005_01C04CA5.30664F20
> Content-Type: text/html;
> 	charset="iso-8859-1"
> Content-Transfer-Encoding: quoted-printable

Um, please try to avoid that stuff. It looks bad in some peoples
mailers, and I think it also messes with the list digests.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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