I have a bunch of output .txt-files that consists of a large parameter list and a X-Y-coordinate set. I need to extract these coordinates from all files so that only those lines are imported to a vector. This would work fine with
impcoord<-read.table("file.txt",skip= ,nrow= ,...)
but the files print the coordinate sets after different lengths of supporting parameters.
Luckily the coordinates always start after a line containing certain words.
Thus my question is, how do I start reading the .txt-file after these words? Let's say they are:
coordinatesXY
Thanks alot for your time and help!
-Olli
--Edit--
Sorry for the confusion.
The part of the file is as follows:
##XYDATA= (X++(Y..Y))
131071 -2065
131070 -4137
131069 -6408
131068 -8043
... ...
... ...
The first line being the one where skip
should end and the following coordinates need to be imported to a vector. As you can see the X-coordinates start from 131071 and end to 0.
1) read.pattern
read.pattern
in gsubfn can be used to read only lines matching a specific pattern. In this example we match beginning of line, optional space(s), 1 or more digits, 1 or more spaces, an optional minus followed by 1 or more digits, optional space(s), end of line. The portions matching the parenthesized portions of the regexp are returned as columns in a data.frame.text = Lines
in this self contained example can be replaced with"myfile.txt"
, say, if the data is coming from a file. Modify the pattern to suit.giving:
2) read twice Another possibility using only base R is simply to read it once to determine the value of
skip=
and a second time to do the actual read using that value. To read from a filemyfile.txt
replacetext = Lines
andtextConnection(Lines)
with"myfile.txt"
.Added Some revisions and added second approach.
This looks like a job for
data.table
'sfread
--edit--
That is why it is good to give a reproducible example. That error means your file is causing trouble.
The skip command matches the text you give it to the file to identify what line to start at, so you need to give it a unique string from the start of the line that you want it to start reading from. That function would work for something like this:
An possible approach could be the following:
You read one line at the time from the input file and stop when you find the indicator string. Then you read the file through
read.table
. With this approach you don't store the entire file in memory, but just the piece you need.