Skip to content

R help for a newby

2 messages · tmed, Peter Dalgaard

#
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 
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=c(10000,5)) this looks ok. 
The next thing I did was to scan in the data.
dat <-scan("mydata-file",what=array(""),sep="\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 
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.

Thank you
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://stat.ethz.ch/pipermail/r-help/attachments/20001112/f981c178/attachment.html
#
tmed <tmed at pacbell.net> writes:
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.
Um, please try to avoid that stuff. It looks bad in some peoples
mailers, and I think it also messes with the list digests.