[R] read complicated file
jim holtman
jholtman at gmail.com
Thu Nov 15 19:06:23 CET 2007
Here is one way of doing it. I put your data in a file and then read
it in my lines and deleted everything upto END and then used
textConnection to read the rest of the data. When I looked at the
first 82 values, then seem to have started at the correct place, but I
did see the value you expected in the 82th location. So this can be a
start on what you need:
> x <- readLines('/tempxx.txt')
> # find the END line
> end.loc <- grep("^END", x)
> # delete all the lines from the beginning to here
> x <- x[-seq_len(end.loc)]
> # now read in your data
> Input <- textConnection(x)
> # read in 82 values after skipping 3
> testInput <- scan(Input, what=0, nmax=82, skip=3)
Read 82 items
> testInput
[1] 118 -101 -84 56 72 157 -158 77 21 -238 -171 -257 -34
78 -228 -122 328 144 23 -168
[21] 159 106 -60 330 -139 33 -22 215 95 -89 -201 199 364
-70 352 -25 -108 -100 23 105
[41] -42 106 164 123 289 77 50 16 -132 51 140 105 229
135 -171 75 83 -165 133 -131
[61] -8 -132 149 165 60 31 -305 336 -16 73 -10 212 65
12 193 180 -82 137 7 -146
[81] 249 59
>
>
On Nov 15, 2007 6:09 AM, William Simpson <william.a.simpson at gmail.com> wrote:
> Dear R experts,
>
> I have been given data files in the following configuration and have
> been puzzling about how to read them in. First I will give a snippet
> of the beginning of file:
>
> Data File: W
> Para File: GABOR_0.gor v 10.6,
> Date : 29/10/2007
> Time : 13:33
> 3.00
> 5.000
> Noise SD (deg):
> 15.000
> 1 -5.321 -5.321
> 2 -5.321 -3.991
> 3 -5.321 -2.661
> 4 -5.321 -1.330
> 5 -5.321 0.000
> 6 -5.321 1.330
> 7 -5.321 2.661
> 8 -5.321 3.991
> 9 -5.321 5.321
> 10 -3.991 -5.321
> 11 -3.991 -3.991
> 12 -3.991 -2.661
> 13 -3.991 -1.330
> 14 -3.991 0.000
> 15 -3.991 1.330
> 16 -3.991 2.661
> 17 -3.991 3.991
> 18 -3.991 5.321
> 19 -2.661 -5.321
> 20 -2.661 -3.991
> 21 -2.661 -2.661
> 22 -2.661 -1.330
> 23 -2.661 0.000
> 24 -2.661 1.330
> 25 -2.661 2.661
> 26 -2.661 3.991
> 27 -2.661 5.321
> 28 -1.330 -5.321
> 29 -1.330 -3.991
> 30 -1.330 -2.661
> 31 -1.330 -1.330
> 32 -1.330 0.000
> 33 -1.330 1.330
> 34 -1.330 2.661
> 35 -1.330 3.991
> 36 -1.330 5.321
> 37 0.000 -5.321
> 38 0.000 -3.991
> 39 0.000 -2.661
> 40 0.000 -1.330
> 41 0.000 0.000
> 42 0.000 1.330
> 43 0.000 2.661
> 44 0.000 3.991
> 45 0.000 5.321
> 46 1.330 -5.321
> 47 1.330 -3.991
> 48 1.330 -2.661
> 49 1.330 -1.330
> 50 1.330 0.000
> 51 1.330 1.330
> 52 1.330 2.661
> 53 1.330 3.991
> 54 1.330 5.321
> 55 2.661 -5.321
> 56 2.661 -3.991
> 57 2.661 -2.661
> 58 2.661 -1.330
> 59 2.661 0.000
> 60 2.661 1.330
> 61 2.661 2.661
> 62 2.661 3.991
> 63 2.661 5.321
> 64 3.991 -5.321
> 65 3.991 -3.991
> 66 3.991 -2.661
> 67 3.991 -1.330
> 68 3.991 0.000
> 69 3.991 1.330
> 70 3.991 2.661
> 71 3.991 3.991
> 72 3.991 5.321
> 73 5.321 -5.321
> 74 5.321 -3.991
> 75 5.321 -2.661
> 76 5.321 -1.330
> 77 5.321 0.000
> 78 5.321 1.330
> 79 5.321 2.661
> 80 5.321 3.991
> 81 5.321 5.321
> END
> 1
> 5.000
> 15.000
> 118 -101 -84 56 72 157 -158 77 21 -238 -171 -257 -34 78 -228 -122 328 144 23 -168 159 106 -60 330 -139 33 -22 215 95 -89 -201 199 364 -70 352 -25 -108 -100 23 105 -42 106 164 123 289 77 50 16 -132 51 140 105 229 135 -171 75 83 -165 133 -131 -8 -132 149 165 60 31 -305 336 -16 73 -10 212 65 12 193 180 -82 137 7 -146 249
> 59 -180 -73 -278 -124 -22 107 164 73 160 -136 -37 119 -10 100 -4 0 182 152 35 256 70 148 -9 -4 0 49 128 -44 21 36 143 -114 -59 -110 7 -40 -80 -70 99 27 -27 184 293 257 -83 44 101 65 -68 -167 158 94 -39 130 59 -349 34 47 -108 70 141 55 138 -20 -83 81 -15 74 -107 140 -280 107 -325 83 125 -64 200 -122 123 -280
> 22
> 2
> 5.000
> 15.000
> 93 313 312 -113 230 160 -13 -42 145 -31 184 -287 -92 5 48 -62 5 110 -58 215 73 -171 -15 219 -20 94 -37 -13 -198 175 -179 12 -47 27 186 -180 30 0 -25 -91 164 117 -155 188 149 -28 24 5 20 -31 52 -78 45 -133 -63 -77 75 -183 130 -119 -47 -8 -40 64 209 166 48 -65 -244 111 110 -106 -248 -21 -173 2 -38 111 30 -174 257
> 59 -180 -73 -278 -124 -22 107 164 73 160 -136 -37 119 -10 100 -4 0 182 152 35 256 70 148 -9 -4 0 49 128 -44 21 36 143 -114 -59 -110 7 -40 -80 -70 99 27 -27 184 293 257 -83 44 101 65 -68 -167 158 94 -39 130 59 -349 34 47 -108 70 141 55 138 -20 -83 81 -15 74 -107 140 -280 107 -325 83 125 -64 200 -122 123 -280
> 21
> ...
>
> The first bit up to "END" can be skipped. That's the first 90 lines.
>
> Then I need to do something like this:
> while data still exist in the file
> {
> skip 3 lines
> scan 81 values into temp
> scan 82nd value, which is 11, 12, 21, 22. Depending on value, temp is
> added to one of these vars
> }
>
> The data are written in clumps.
> Each clump has 3 lines with info I don't need.
> Then it has 81 values which are the actual data I want to read into
> some variable "temp"
> Then the 82nd value tells me which of 4 variables to add temp onto.
>
> Any tips on how to approach this using scan() greatly appreciated. I
> know I can use skip as an argument to scan.
>
> Thanks very much for any help!
>
> Bill
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
More information about the R-help
mailing list