[R] R help for a newby
ben@zoo.ufl.edu
ben at zoo.ufl.edu
Mon Nov 13 23:35:29 CET 2000
I don't actually think there's an easy way to do this; an alternate way
might be to reformat your data either with fixed-width fields (read.fwf())
or with separator characters. If you insist, though, here is a (probably
overly convoluted) way of doing what I think you want. I would welcome
suggestions for making this more efficient or understandable.
scan.irreg <- function(...) {
row.lens <- count.fields(...) # count fields per line
x <- scan(...) # read data into a vector
maxcol <- max(row.lens) # largest # of columns
nrows <- length(row.lens) # total # of rows
y <- matrix(nrow=nrows,ncol=maxcol)
ind1 <- seq(1,length=nrows,by=maxcol)
ind2 <- cbind(ind1,ind1+row.lens-1)
ind3 <- unlist(apply(ind2,1,function(x)seq(from=x[1],to=x[2])))
ty <- t(y)
ty[ind3] <- x
t(ty)
}
Arguably it would be more efficient and more "R-like" (and certainly
easier to code) to return the answers as a list of variable-length
vectors:
scan.irreg2 <- function(...) {
row.lens <- count.fields(...) # count fields per line
x <- scan(...) # read data into a vector
nrows <- length(row.lens) # total # of rows
split(x,rep(1:nrows,row.lens))
}
You can then use apply or sapply to operate on the elements of the list.
On Mon, 13 Nov 2000, Thomas Likins wrote:
> Hello,
>
> Yesterday I asked a question about how to read data from a file
> that had test numbers in the first column followed by 4 readings. I received
> three responses to my question with in an hour. Thanks to all who responded.
> After trying out your responses - which seem to work. I noticed that some of
> the data had only one or two reading after the test number.
>
> My next question: what's the command to fill a matrix with an unknown
> value of column data. The nice people who responded to me yesterday used
> matrix(scan("data.file"),rcol=5, byrow=TRUE). I would have to imagine the
> solution is some variation on this theme. I tried several last night
> myself, but
> with no success. I hope that you will bear with me though this leaning curve.
> I HAVE tried to read the documentation, but there is so much information that
> it's some what overwhelming. I'm sure all of you know how important it
> is to be able to succeed in using a new tool to prevent being frustrated and
> giving up prematurely. I am truly in awe over this tool, its power is
> amazing.
> I hope you will bear with me as I continue in the mastery of this incredible
> tool.
>
> Thanks for all your help,
>
> Tom Likins
>
>
>
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>
>
--
318 Carr Hall bolker at zoo.ufl.edu
Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker
Box 118525 (ph) 352-392-5697
Gainesville, FL 32611-8525 (fax) 352-392-3704
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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