[R] sscanf equivalent

Paul Roebuck roebuck at wotan.mdacc.tmc.edu
Fri Oct 7 19:22:49 CEST 2005


I have a data file from which I need to read portions of
data but data location/quantity can change from file to file.
I wrote some code and have a working solution but it seems
wasteful to have to do it this way. Here's the contrived
incomplete code.

    datalines <- readLines(datafile.pathname)
    # marker will appear on line preceding and following
    # actual data
    offset.data <- grep("marker", datalines)
    datalines <- NULL

    # grab first column of each assoc dataline
    data <- scan(datafile.pathname,
                 what = numeric(0),
                 skip = offset.data[1],
                 nlines = offset.data[2]-offset.data[1]-1,
                 flush = TRUE,
                 multi.line = FALSE,
                 quiet = TRUE)
    # output is vector of values

Originally wrote code to parse data from 'datalines'
using sub and strsplit methods but it was woefully slower
and more complex than using scan method. What is desired
is a means of invoking method like scan but with existing
data instead of filename.

----------------------------------------------------------
SIGSIG -- signature too long (core dumped)




More information about the R-help mailing list