[R] Reading multiple tables from one .txt doc

Bert Gunter bgunter.4567 at gmail.com
Tue May 3 17:41:03 CEST 2016


One approach would be to use ?readLines to read the lines into a
character vector. You could then use indexing to remove all the blank
and header (lines beginning with "image") lines. You can now find the
indices of where the separate data blocks begin (they all begin with
"#", right?) and then sequentially read them into a list of data
frames, using ?strsplit to delineate columns and ?as.numeric to
convert the numeric columns. I leave it to you to work out what I
think are the straightforward details.

Do note, however, that there may be better ways to do this, especially
using some of the text manipulation packages and functions.  Have a
look at the "stringr" package or any others that searching might bring
up (rseek.org is a good search site for R; as is google of course).
And wait a bit for better suggestions before proceeding, as my brute
force approach should probably be considered a last resort.


Cheers,
Bert




Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Tue, May 3, 2016 at 7:45 AM, Mark Fingerle
<mark.fingerle at brainlab.com> wrote:
> Dear all,
> I have a .txt file which contains multiple tables and I would like to read these tables separately in order to create graphs for each one.
> The tables are separated by a blank line, have a variable number of lines, fixed nr. Of rows, have a header and a comment above the header (#) which contains a specific word that identifies each table. (see example below). It would be possible to change the layout of the .txt data a bit (Add a word, remove comment etc..)
> I would be extremely grateful if anyone could help me with this daunting task :-)
> Example:
> # CoordA
> Image    X             Y             Z             MeasuredMove MachineMove
> vf_36.png            -114.345475       -89.043448         556.073402         0             0
> vf_37.png            -111.118715       -89.978534         606.040764         50.080172           50.000000
> vf_38.png            -107.911209       -90.901958         656.025557         50.096111           50.000000
> vf_39.png            -104.693931       -91.814392         705.982620         50.068868           50.000000
> vf_40.png            -101.459549       -92.730113         755.983835         50.114082           50.000000
>
> # CoordB
> Image    X             Y             Z             MeasuredMove MachineMove
> vf_36.png            -115.345475       -89.043448         556.073402         0             0
> vf_37.png            -115.118715       -89.978534         606.040764         50.080172           50.000000
> vf_38.png            -134.911209       -90.901958         656.025557         50.096111           50.000000
> vf_39.png            -164.693931       -91.814392         705.982620         50.068868           50.000000
> vf_40.png            -134.459549       -92.730113         755.983835         50.114082           50.000000
>
> # CoordC
> Image    X             Y             Z             MeasuredMove MachineMove
> vf_36.png            -168.345475       -89.043448         556.073402         0             0
> vf_37.png            -115.118715       -89.978534         606.040764         50.080172           50.000000
> vf_38.png            -146.911209       -90.901958         656.025557         50.096111           50.000000
> vf_39.png            -187.693931       -91.814392         705.982620         50.068868           50.000000
> vf_40.png            -185.459549       -92.730113         755.983835         50.114082           50.000000
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list