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
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
At 10:08 20/12/2002 +1300, vous avez ?crit:>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, > >KevinHere is a possible solution (if your data file is `yourfile.txt'): tmp <- scan("yourfile.txt", what = "", sep = "\n") skp <- grep("@DATA", tmp) your.data <- scan("yourfile.txt", what = [...], skip = skp) You may improve this by scanning line by line with: scan("foo.txt", what = "", sep = "\n", n = 1, skip = s) with s = 0, 1, 2, ... till you meet "@DATA". Hope this helps. EP Emmanuel Paradis Laboratoire de Pal?ontologie Institut des Sciences de l'?volution Universit? Montpellier II F-34095 Montpellier c?dex 05 France phone: +33 4 67 14 39 64 fax: +33 4 67 14 36 10 mailto:paradis at isem.univ-montp2.fr http://www.isem.univ-montp2.fr/ISEMFre/Equipes/PPP/PPerso/ParadisE.php