Reputation: 3
I have problems when I try to import data with multiple headers.
There is the file:
Ry1_Ch_1_2013-8-23_21-56-49.txt
100.00 9918.21
36.89 7247.92
21.50 11825.56
Ry2_Ch_2_2013-8-23_21-56-49.txt
100.00 6103.52
97.81 7247.92
79.96 9536.74
78.73 3433.23
29.11 1144.41
28.85 12207.03
26.13 12969.97
24.75 50000000.00
16.26 34332.28
.
.
.
Ry28_Ch_1_2013-8-23_22-0-11.txt
100.00 10299.68
40.35 4577.64
26.50 3433.23
15.27 6484.99
The file has multiple blank spaces and headers terminated with .txt string.
Each header has 2 columns with data that I want to plot and analyse. I suggest to save each table in a variable, then, working with it.
I try to use read.table command from R but not had good results.
Upvotes: 0
Views: 148
Reputation: 263362
txt <- readLines(textConnection(" Ry1_Ch_1_2013-8-23_21-56-49.txt
100.00 9918.21
36.89 7247.92
21.50 11825.56
Ry2_Ch_2_2013-8-23_21-56-49.txt
100.00 6103.52
97.81 7247.92
79.96 9536.74
78.73 3433.23
29.11 1144.41
28.85 12207.03
26.13 12969.97
24.75 50000000.00
16.26 34332.28
.
.
.
Ry28_Ch_1_2013-8-23_22-0-11.txt
100.00 10299.68
40.35 4577.64
26.50 3433.23
15.27 6484.99"))
Will exclude lines with fewer than 7 characters and use grepl
matches to ".txt" to mark 'section' beginnings. The 'section' vaariable is just the cumsum of those hits so lines between occurrences of '.txt' will all have the same section number. The read.table within groups of the same 'section':
section <- cumsum( grepl("txt", txt[nchar(txt)>7]) )
lapply( split(txt[nchar(txt)>7] , section),
function(t) read.table(text=t, skip=1) )
$`1`
V1 V2
1 100.00 9918.21
2 36.89 7247.92
3 21.50 11825.56
$`2`
V1 V2
1 100.00 6103.52
2 97.81 7247.92
3 79.96 9536.74
4 78.73 3433.23
5 29.11 1144.41
6 28.85 12207.03
7 26.13 12969.97
8 24.75 50000000.00
9 16.26 34332.28
$`3`
V1 V2
1 100.00 10299.68
2 40.35 4577.64
3 26.50 3433.23
4 15.27 6484.99
So store these as a list and name then something
readList <- .Last.value
names(readList) <- txt[ grepl(".txt", txt) ]
> str(readList)
List of 3
$ Ry1_Ch_1_2013-8-23_21-56-49.txt :'data.frame': 3 obs. of 2 variables:
..$ V1: num [1:3] 100 36.9 21.5
..$ V2: num [1:3] 9918 7248 11826
$ Ry2_Ch_2_2013-8-23_21-56-49.txt:'data.frame': 9 obs. of 2 variables:
..$ V1: num [1:9] 100 97.8 80 78.7 29.1 ...
..$ V2: num [1:9] 6104 7248 9537 3433 1144 ...
$ Ry28_Ch_1_2013-8-23_22-0-11.txt:'data.frame': 4 obs. of 2 variables:
..$ V1: num [1:4] 100 40.4 26.5 15.3
..$ V2: num [1:4] 10300 4578 3433 6485
Upvotes: 1