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"))
将排除少于 7 个字符的行,并使用grepl
“.txt”匹配来标记“节”的开头。'section' 变量只是这些命中的总和,因此出现 '.txt' 之间的行都将具有相同的节号。同一“节”组内的 read.table:
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
所以将这些存储为列表并命名然后
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