Skip to content
Prev 8065 / 398506 Next

R help for a newby

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: