[R] how to read this data file into R?

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Wed Feb 3 09:31:54 CET 2010


On Tue, Feb 2, 2010 at 11:40 PM, David Winsemius <dwinsemius at comcast.net> wrote:

> The real solution is to grab the miscreant sender by the throat , er,
>  tactfully discuss with your valued customer ,,, and shake out a machine
> readable form that has all of one row in a row.

Indeed. But you might get away with something else...

It is composed of blocks of (header +  25 data) rows - so using
read.table with skip= set to N*26  and nrows=25 would let you read
each block, and then use cbind to make up a big matrix.

# Here's my test example, which I did with 26 rows just to make sure
you understand it and don't just blindly cut n paste (or maybe I can't
count):

# test - create a matrix and dump it in this format to /tmp/m.txt:
m=matrix(sample(26*40),26,40)
m
sink("/tmp/m.txt")
m
sink()
# now read the second chunk:
read.table("/tmp/m.txt",skip=27,nrows=26,sep="")

# how to do the whole thing:

# gotta have something to bind on to for starters:
> mm=matrix(0,nrow=26,ncol=1)

> for(i in 0:3){
+  mm = cbind(mm,read.table("/tmp/m.txt",skip=i*27,nrows=26,sep=""))
+ }
# get rid of that first column:
> mm=mm[,-1]

# and now
> all(mm==m)
[1] TRUE

 Recovery!


But yes, if someone gave you this file then they done wrong, but
sometimes all you have is an R transcript from the distant past (or
possibly even an old S-plus transcript with an S-plus .Data that you
can't read any more).

Barry

-- 
blog: http://geospaced.blogspot.com/
web: http://www.maths.lancs.ac.uk/~rowlings
web: http://www.rowlingson.com/
twitter: http://twitter.com/geospacedman
pics: http://www.flickr.com/photos/spacedman



More information about the R-help mailing list