[R] Reading complicated data file

John Fox jfox at mcmaster.ca
Wed Sep 25 16:41:42 CEST 2002


Dear Jakub,

At 02:23 PM 9/25/2002 +0200, zlamal at ufi.fme.vutbr.cz wrote:
>My problem is little bit complicate because lines starting 0 or -1 can
>be everywhere in file (there are also lines starting with 2 or -2, etc...)
>so structure of file can be
>
>0 something
>-1 something
>1 1 3
>2 1 5
>-2 something
>0 something
>1 2 5
>1 5 6

I think that I understand now what you want, and the alternative approach 
that I suggested in my original response should work. (Your original 
posting and my response are appended below.) If you want only the rows with 
1 in the first column, then test that column == 1 and use the result to 
subset rows (or, a bit more tediously, process the file one line at a time).

John


>At 01:15 PM 9/25/2002 +0200, zlamal at ufi.fme.vutbr.cz wrote:
>>Hi, I am new in R and I have problem with reading this data file
>>
>>0 TITLE Title
>>  0 XLEGEND Legend
>>-1 LABEL x
>>1 1 12
>>1 2 30
>>1 3 34
>>
>>
>>I want to read only lines starting with 1 (it indicates 1st plotting line)
>>and create data set from second and third value on this row.
>
>If you know in advance where the data starts (i.e., that you need to skip 
>the first 3 rows), then you can read the data file with read.table in the 
>usual manner, but specifying the argument skip=3. This also supposes that 
>the lines to be skipped precede the data.
>
>If the situation is more complicated -- e.g., you don't know in advance 
>how many lines to skip, or the data and non-data lines are intermixed -- 
>then there are several ways to proceed. One way would be to read the data 
>in the usual manner, and then to select rows for which the first column is 
>1; with this approach, you would also have to handle converting columns 
>two and three to numeric. Alternatively, you could read the file 
>line-by-line (see ?readLines).
>

-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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