Skip to content
Prev 69467 / 398513 Next

converting an ASCII file to a matrix

This seems to work but it's a bit ugly with the loop (I'm sure you could
replace the loop with "apply").

asc2mat <- function(fname) {
  x <- sapply(scan(fname, "character", sep="\n"), strsplit, "")
  rlen <- sapply(x, length)
  res <- matrix(nrow=length(bar), ncol=max(rlen))
  
  for (i in 1:nrow(res)) {
     res[i,1:rlen[i]] <- x[[i]]
  }
  return(res)
}
 
Norm Olsen 

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Berton Gunter
Sent: Tuesday, May 10, 2005 9:07 AM
To: 'Michael Graber'; r-help at stat.math.ethz.ch
Subject: RE: [R] converting an ASCII file to a matrix

Michael:

Ah ... the bane of real data analysts everywhere: getting the data from its
original format into (R )- usable form for data analysis 

This has nothing to do with R-WinEdit, AFAICS.

My approach would be to simply use readLines() to read the lines in as
character strings and then process them by grep and/or regexpr() to extract
the bits I wanted. If the formatting is fixed, substring() may also be
useful. You will also need to convert the resulting character representation
of numerics to numerics via as.numeric().

If you haven't worked through regular expressions before (?regexp), you will
find this a bit of a chore; but it is well worth the effort, as they are
invaluable for this sort of thing. There are numerous web tutorials to help
(google on 'regular expressions').

Cheers,

-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
 
"The business of the statistician is to catalyze the scientific learning
process."  - George E. P. Box
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html