[R] More on scan()

apjaworski@mmm.com apjaworski at mmm.com
Thu Dec 19 23:25:03 CET 2002


Here is one way of doing this.
(1) read the whole file in as a vector of strings one line at a time
      x <- readLines("<path to your data file>")
(2) find the position of the "@DATA" string in your vector
      s <- which(x == "@DATA")
(3) scan the file again skipping s lines
      scan("<path to your data file>", skip=s, sep=",", ...)

You may want to consider using read.table instead of scan - it has the skip
parameter too.  Finally you could actually reuse x from (1) above by
something like
      x <- x[-(1:s])
      loop through elements of x and use the strsplit command to extract
numbers from each line
I suspect that this would be more cumbersome and slower than re-reading the
file skiiping s lines from the top.

Cheers,

Andy

__________________________________
Andy Jaworski
Engineering Systems Technology Center
3M Center, 518-1-01
St. Paul, MN 55144-1000
-----
E-mail: apjaworski at mmm.com
Tel:  (651) 733-6092
Fax:  (651) 736-3122


|---------+------------------------------>
|         |           Ko-Kang Kevin Wang |
|         |           <kwan022 at stat.auckl|
|         |           and.ac.nz>         |
|         |           Sent by:           |
|         |           r-help-admin at stat.m|
|         |           ath.ethz.ch        |
|         |                              |
|         |                              |
|         |           12/20/2002 04:08   |
|         |                              |
|---------+------------------------------>
  >-----------------------------------------------------------------------------------------------------------------------------|
  |                                                                                                                             |
  |      To:       R Help <r-help at stat.math.ethz.ch>                                                                            |
  |      cc:                                                                                                                    |
  |      Subject:  [R] More on scan()                                                                                           |
  >-----------------------------------------------------------------------------------------------------------------------------|



Hi,

If I have a CSV file which has several comments at the top, and the data
start immediately after the line:
  @DATA

Is it possible to use the scan() command to get the CSV data into R, by
only reading the lines after @DATA?  If so, how can I do it?

Cheers,

Kevin

------------------------------------------------------------------------------

/* Time is the greatest teacher, unfortunately it kills its students */

Ko-Kang Kevin Wang
Master of Science (MSc) Student
Department of Statistics
University of Auckland
New Zealand
Homepage: http://www.stat.auckland.ac.nz/~kwan022

______________________________________________
R-help at stat.math.ethz.ch mailing list
http://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list