从 *.txt 输出文件中提取数据到 R
Extracting data from *.txt output file into R
我有一个 output.txt 文件,它来自我在 R 中进行的分析。我想提取:
- 每个主题的 .txt 文件中的
<< Output >>
表,并将它们组合到一个 R 数据框中。每个主题之间的输出列名称是一致的。
<< Predicted Output >>
相同,并在 R 中组合成一个数据框。
如前所述,输出文件中间有我不需要的文本。
为了便于查看 output.txt 文件的结构,我已将 .txt 文件上传到以下 link here。我还在下面放了一张截图来展示输出的结构。
我尝试使用类似的东西来做到这一点,但没有成功:
df <- read.delim("ivivc_outputs.txt").
试试这个开始。如果需要,添加更多条件。我希望这是有帮助的。如果您有任何需要,请随时提出。
b = readLines(file('ivivc_outputs.txt', 'r'))
n_out = 1
n_pred = 1
listOutput = list()
listPredictOutput = list()
for(i in 1:length(b)){
if(b[i] == "<< Output >>"){
a = strsplit(b[i+1], " ")[[1]]
a = a[a != ""]
# print(a)
df <- data.frame(matrix(ncol = length(a), nrow = 0))
colnames(df) = a
control = 2
while(control != 20){
l = strsplit(b[i+control], " ")[[1]]
l = l[l != ""]
df[control-1,] = l
control = control + 1
}
listOutput[[n_out]] = df
n_out = n_out+1
}
if(b[i] == "<< Predicted Output >>"){
a = strsplit(b[i+1], " ")[[1]]
a = a[a != ""]
# print(a)
df <- data.frame(matrix(ncol = length(a), nrow = 0))
colnames(df) = a
control = 2
while(control != 20){
l = strsplit(b[i+control], " ")[[1]]
l = l[l != ""]
df[control-1,] = l
control = control + 1
}
listPredictOutput[[n_pred]] = df
n_pred = n_pred+1
}
}
# to merge all data frames use `bind_rows` from `dplyr`
library(dplyr)
dfOutput = bind_rows(listOutput)
dfPredictOutput = bind_rows(listPredictOutput)
> dfPredictOutput
# pH subj formula. time FABpred conc.pred AUCpred
# 1 1.2 1 capsule 0.0 0.000000 0.00000 0.00000
# 2 1.2 1 capsule 1.0 2.528737 8.39300 4.19650
# 3 1.2 1 capsule 2.0 7.415708 22.57987 19.68293
# 4 1.2 1 capsule 3.0 15.845734 45.08950 53.51761
# 5 1.2 1 capsule 4.0 24.275759 62.14611 107.13542
# 6 1.2 1 capsule 5.0 33.133394 76.48998 176.45346
# 7 1.2 1 capsule 6.0 41.991029 87.35901 258.37796
# 8 1.2 1 capsule 6.5 45.900606 89.90799 302.69471
# 9 1.2 1 capsule 7.0 49.810183 92.12832 348.20378
# 10 1.2 1 capsule 7.5 53.719760 94.06237 394.75146
# 11 1.2 1 capsule 8.0 57.629337 95.74705 442.20381
# 12 1.2 1 capsule 8.0 57.629337 95.74705 442.20381
# 13 1.2 1 capsule 9.0 63.860225 93.23271 536.69369
# 14 1.2 1 capsule 10.0 70.091113 91.32747 628.97378
# 15 1.2 1 capsule 12.0 79.498532 79.70975 800.01101
# 16 1.2 1 capsule 16.0 90.372043 49.52751 1058.48553
# 17 1.2 1 capsule 20.0 101.245554 40.79704 1239.13462
# 18 1.2 1 capsule 24.0 107.354268 26.67212 1374.07293
# 19 1.2 1 capsuleContent 0.0 0.000000 0.000000 0.000000
# 20 1.2 1 capsuleContent 1.0 1.490256 4.946231 2.473115
# 21 1.2 1 capsuleContent 2.0 5.338746 16.521316 13.206889
# 22 1.2 1 capsuleContent 3.0 13.341161 39.079386 41.007240
# 23 1.2 1 capsuleContent 4.0 21.343576 56.172708 88.633287
# 24 1.2 1 capsuleContent 5.0 30.017950 71.355393 152.397337
# 25 1.2 1 capsuleContent 6.0 38.692324 82.860036 229.505052
# 26 1.2 1 capsuleContent 6.5 42.571357 85.881180 271.690356
# 27 1.2 1 capsuleContent 7.0 46.450390 88.512793 315.288850
# 28 1.2 1 capsuleContent 7.5 50.329423 90.805099 360.118323
# 29 1.2 1 capsuleContent 8.0 54.208457 92.801847 406.020060
# 30 1.2 1 capsuleContent 8.0 54.208457 92.801847 406.020060
# 31 1.2 1 capsuleContent 9.0 60.439345 91.000989 497.921478
# 32 1.2 1 capsuleContent 10.0 66.670233 89.636393 588.240168
# 33 1.2 1 capsuleContent 12.0 76.322001 79.472871 757.349432
# 34 1.2 1 capsuleContent 16.0 87.256599 49.607701 1015.510577
# 35 1.2 1 capsuleContent 20.0 98.191197 40.968947 1196.663873
# 36 1.2 1 capsuleContent 24.0 104.177736 26.424419 1331.450604
# 37 1.2 2 capsule 0.0 0.000000 0.00000 0.00000
# 38 1.2 2 capsule 1.0 2.528737 8.39300 4.19650
# 39 1.2 2 capsule 2.0 7.415708 22.57987 19.68293
# 40 1.2 2 capsule 3.0 15.845734 45.08950 53.51761
# 41 1.2 2 capsule 4.0 24.275759 62.14611 107.13542
# 42 1.2 2 capsule 5.0 33.133394 76.48998 176.45346
# 43 1.2 2 capsule 6.0 41.991029 87.35901 258.37796
# 44 1.2 2 capsule 6.5 45.900606 89.90799 302.69471
# 45 1.2 2 capsule 7.0 49.810183 92.12832 348.20378
# 46 1.2 2 capsule 7.5 53.719760 94.06237 394.75146
# 47 1.2 2 capsule 8.0 57.629337 95.74705 442.20381
# 48 1.2 2 capsule 8.0 57.629337 95.74705 442.20381
# 49 1.2 2 capsule 9.0 63.860225 93.23271 536.69369
# 50 1.2 2 capsule 10.0 70.091113 91.32747 628.97378
# 51 1.2 2 capsule 12.0 79.498532 79.70975 800.01101
# 52 1.2 2 capsule 16.0 90.372043 49.52751 1058.48553
# 53 1.2 2 capsule 20.0 101.245554 40.79704 1239.13462
# 54 1.2 2 capsule 24.0 107.354268 26.67212 1374.07293
# 55 1.2 2 capsuleContent 0.0 0.000000 0.000000 0.000000
# 56 1.2 2 capsuleContent 1.0 1.490256 4.946231 2.473115
# 57 1.2 2 capsuleContent 2.0 5.338746 16.521316 13.206889
# 58 1.2 2 capsuleContent 3.0 13.341161 39.079386 41.007240
# 59 1.2 2 capsuleContent 4.0 21.343576 56.172708 88.633287
# 60 1.2 2 capsuleContent 5.0 30.017950 71.355393 152.397337
# 61 1.2 2 capsuleContent 6.0 38.692324 82.860036 229.505052
# 62 1.2 2 capsuleContent 6.5 42.571357 85.881180 271.690356
# 63 1.2 2 capsuleContent 7.0 46.450390 88.512793 315.288850
# 64 1.2 2 capsuleContent 7.5 50.329423 90.805099 360.118323
# 65 1.2 2 capsuleContent 8.0 54.208457 92.801847 406.020060
# 66 1.2 2 capsuleContent 8.0 54.208457 92.801847 406.020060
# 67 1.2 2 capsuleContent 9.0 60.439345 91.000989 497.921478
# 68 1.2 2 capsuleContent 10.0 66.670233 89.636393 588.240168
# 69 1.2 2 capsuleContent 12.0 76.322001 79.472871 757.349432
# 70 1.2 2 capsuleContent 16.0 87.256599 49.607701 1015.510577
# 71 1.2 2 capsuleContent 20.0 98.191197 40.968947 1196.663873
# 72 1.2 2 capsuleContent 24.0 104.177736 26.424419 1331.450604
# 73 1.2 3 capsule 0.0 0.000000 0.00000 0.00000
# 74 1.2 3 capsule 1.0 2.528737 8.39300 4.19650
# 75 1.2 3 capsule 2.0 7.415708 22.57987 19.68293
# 76 1.2 3 capsule 3.0 15.845734 45.08950 53.51761
# 77 1.2 3 capsule 4.0 24.275759 62.14611 107.13542
# 78 1.2 3 capsule 5.0 33.133394 76.48998 176.45346
# 79 1.2 3 capsule 6.0 41.991029 87.35901 258.37796
# 80 1.2 3 capsule 6.5 45.900606 89.90799 302.69471
# 81 1.2 3 capsule 7.0 49.810183 92.12832 348.20378
# 82 1.2 3 capsule 7.5 53.719760 94.06237 394.75146
# 83 1.2 3 capsule 8.0 57.629337 95.74705 442.20381
# 84 1.2 3 capsule 8.0 57.629337 95.74705 442.20381
# 85 1.2 3 capsule 9.0 63.860225 93.23271 536.69369
# 86 1.2 3 capsule 10.0 70.091113 91.32747 628.97378
# 87 1.2 3 capsule 12.0 79.498532 79.70975 800.01101
# 88 1.2 3 capsule 16.0 90.372043 49.52751 1058.48553
# 89 1.2 3 capsule 20.0 101.245554 40.79704 1239.13462
# 90 1.2 3 capsule 24.0 107.354268 26.67212 1374.07293
# 91 1.2 3 capsuleContent 0.0 0.000000 0.000000 0.000000
# 92 1.2 3 capsuleContent 1.0 1.490256 4.946231 2.473115
# 93 1.2 3 capsuleContent 2.0 5.338746 16.521316 13.206889
# 94 1.2 3 capsuleContent 3.0 13.341161 39.079386 41.007240
# 95 1.2 3 capsuleContent 4.0 21.343576 56.172708 88.633287
# 96 1.2 3 capsuleContent 5.0 30.017950 71.355393 152.397337
# 97 1.2 3 capsuleContent 6.0 38.692324 82.860036 229.505052
# 98 1.2 3 capsuleContent 6.5 42.571357 85.881180 271.690356
# 99 1.2 3 capsuleContent 7.0 46.450390 88.512793 315.288850
# 100 1.2 3 capsuleContent 7.5 50.329423 90.805099 360.118323
# 101 1.2 3 capsuleContent 8.0 54.208457 92.801847 406.020060
# 102 1.2 3 capsuleContent 8.0 54.208457 92.801847 406.020060
# 103 1.2 3 capsuleContent 9.0 60.439345 91.000989 497.921478
# 104 1.2 3 capsuleContent 10.0 66.670233 89.636393 588.240168
# 105 1.2 3 capsuleContent 12.0 76.322001 79.472871 757.349432
# 106 1.2 3 capsuleContent 16.0 87.256599 49.607701 1015.510577
# 107 1.2 3 capsuleContent 20.0 98.191197 40.968947 1196.663873
# 108 1.2 3 capsuleContent 24.0 104.177736 26.424419 1331.450604
我有一个 output.txt 文件,它来自我在 R 中进行的分析。我想提取:
- 每个主题的 .txt 文件中的
<< Output >>
表,并将它们组合到一个 R 数据框中。每个主题之间的输出列名称是一致的。 << Predicted Output >>
相同,并在 R 中组合成一个数据框。
如前所述,输出文件中间有我不需要的文本。
为了便于查看 output.txt 文件的结构,我已将 .txt 文件上传到以下 link here。我还在下面放了一张截图来展示输出的结构。
我尝试使用类似的东西来做到这一点,但没有成功:
df <- read.delim("ivivc_outputs.txt").
试试这个开始。如果需要,添加更多条件。我希望这是有帮助的。如果您有任何需要,请随时提出。
b = readLines(file('ivivc_outputs.txt', 'r'))
n_out = 1
n_pred = 1
listOutput = list()
listPredictOutput = list()
for(i in 1:length(b)){
if(b[i] == "<< Output >>"){
a = strsplit(b[i+1], " ")[[1]]
a = a[a != ""]
# print(a)
df <- data.frame(matrix(ncol = length(a), nrow = 0))
colnames(df) = a
control = 2
while(control != 20){
l = strsplit(b[i+control], " ")[[1]]
l = l[l != ""]
df[control-1,] = l
control = control + 1
}
listOutput[[n_out]] = df
n_out = n_out+1
}
if(b[i] == "<< Predicted Output >>"){
a = strsplit(b[i+1], " ")[[1]]
a = a[a != ""]
# print(a)
df <- data.frame(matrix(ncol = length(a), nrow = 0))
colnames(df) = a
control = 2
while(control != 20){
l = strsplit(b[i+control], " ")[[1]]
l = l[l != ""]
df[control-1,] = l
control = control + 1
}
listPredictOutput[[n_pred]] = df
n_pred = n_pred+1
}
}
# to merge all data frames use `bind_rows` from `dplyr`
library(dplyr)
dfOutput = bind_rows(listOutput)
dfPredictOutput = bind_rows(listPredictOutput)
> dfPredictOutput
# pH subj formula. time FABpred conc.pred AUCpred
# 1 1.2 1 capsule 0.0 0.000000 0.00000 0.00000
# 2 1.2 1 capsule 1.0 2.528737 8.39300 4.19650
# 3 1.2 1 capsule 2.0 7.415708 22.57987 19.68293
# 4 1.2 1 capsule 3.0 15.845734 45.08950 53.51761
# 5 1.2 1 capsule 4.0 24.275759 62.14611 107.13542
# 6 1.2 1 capsule 5.0 33.133394 76.48998 176.45346
# 7 1.2 1 capsule 6.0 41.991029 87.35901 258.37796
# 8 1.2 1 capsule 6.5 45.900606 89.90799 302.69471
# 9 1.2 1 capsule 7.0 49.810183 92.12832 348.20378
# 10 1.2 1 capsule 7.5 53.719760 94.06237 394.75146
# 11 1.2 1 capsule 8.0 57.629337 95.74705 442.20381
# 12 1.2 1 capsule 8.0 57.629337 95.74705 442.20381
# 13 1.2 1 capsule 9.0 63.860225 93.23271 536.69369
# 14 1.2 1 capsule 10.0 70.091113 91.32747 628.97378
# 15 1.2 1 capsule 12.0 79.498532 79.70975 800.01101
# 16 1.2 1 capsule 16.0 90.372043 49.52751 1058.48553
# 17 1.2 1 capsule 20.0 101.245554 40.79704 1239.13462
# 18 1.2 1 capsule 24.0 107.354268 26.67212 1374.07293
# 19 1.2 1 capsuleContent 0.0 0.000000 0.000000 0.000000
# 20 1.2 1 capsuleContent 1.0 1.490256 4.946231 2.473115
# 21 1.2 1 capsuleContent 2.0 5.338746 16.521316 13.206889
# 22 1.2 1 capsuleContent 3.0 13.341161 39.079386 41.007240
# 23 1.2 1 capsuleContent 4.0 21.343576 56.172708 88.633287
# 24 1.2 1 capsuleContent 5.0 30.017950 71.355393 152.397337
# 25 1.2 1 capsuleContent 6.0 38.692324 82.860036 229.505052
# 26 1.2 1 capsuleContent 6.5 42.571357 85.881180 271.690356
# 27 1.2 1 capsuleContent 7.0 46.450390 88.512793 315.288850
# 28 1.2 1 capsuleContent 7.5 50.329423 90.805099 360.118323
# 29 1.2 1 capsuleContent 8.0 54.208457 92.801847 406.020060
# 30 1.2 1 capsuleContent 8.0 54.208457 92.801847 406.020060
# 31 1.2 1 capsuleContent 9.0 60.439345 91.000989 497.921478
# 32 1.2 1 capsuleContent 10.0 66.670233 89.636393 588.240168
# 33 1.2 1 capsuleContent 12.0 76.322001 79.472871 757.349432
# 34 1.2 1 capsuleContent 16.0 87.256599 49.607701 1015.510577
# 35 1.2 1 capsuleContent 20.0 98.191197 40.968947 1196.663873
# 36 1.2 1 capsuleContent 24.0 104.177736 26.424419 1331.450604
# 37 1.2 2 capsule 0.0 0.000000 0.00000 0.00000
# 38 1.2 2 capsule 1.0 2.528737 8.39300 4.19650
# 39 1.2 2 capsule 2.0 7.415708 22.57987 19.68293
# 40 1.2 2 capsule 3.0 15.845734 45.08950 53.51761
# 41 1.2 2 capsule 4.0 24.275759 62.14611 107.13542
# 42 1.2 2 capsule 5.0 33.133394 76.48998 176.45346
# 43 1.2 2 capsule 6.0 41.991029 87.35901 258.37796
# 44 1.2 2 capsule 6.5 45.900606 89.90799 302.69471
# 45 1.2 2 capsule 7.0 49.810183 92.12832 348.20378
# 46 1.2 2 capsule 7.5 53.719760 94.06237 394.75146
# 47 1.2 2 capsule 8.0 57.629337 95.74705 442.20381
# 48 1.2 2 capsule 8.0 57.629337 95.74705 442.20381
# 49 1.2 2 capsule 9.0 63.860225 93.23271 536.69369
# 50 1.2 2 capsule 10.0 70.091113 91.32747 628.97378
# 51 1.2 2 capsule 12.0 79.498532 79.70975 800.01101
# 52 1.2 2 capsule 16.0 90.372043 49.52751 1058.48553
# 53 1.2 2 capsule 20.0 101.245554 40.79704 1239.13462
# 54 1.2 2 capsule 24.0 107.354268 26.67212 1374.07293
# 55 1.2 2 capsuleContent 0.0 0.000000 0.000000 0.000000
# 56 1.2 2 capsuleContent 1.0 1.490256 4.946231 2.473115
# 57 1.2 2 capsuleContent 2.0 5.338746 16.521316 13.206889
# 58 1.2 2 capsuleContent 3.0 13.341161 39.079386 41.007240
# 59 1.2 2 capsuleContent 4.0 21.343576 56.172708 88.633287
# 60 1.2 2 capsuleContent 5.0 30.017950 71.355393 152.397337
# 61 1.2 2 capsuleContent 6.0 38.692324 82.860036 229.505052
# 62 1.2 2 capsuleContent 6.5 42.571357 85.881180 271.690356
# 63 1.2 2 capsuleContent 7.0 46.450390 88.512793 315.288850
# 64 1.2 2 capsuleContent 7.5 50.329423 90.805099 360.118323
# 65 1.2 2 capsuleContent 8.0 54.208457 92.801847 406.020060
# 66 1.2 2 capsuleContent 8.0 54.208457 92.801847 406.020060
# 67 1.2 2 capsuleContent 9.0 60.439345 91.000989 497.921478
# 68 1.2 2 capsuleContent 10.0 66.670233 89.636393 588.240168
# 69 1.2 2 capsuleContent 12.0 76.322001 79.472871 757.349432
# 70 1.2 2 capsuleContent 16.0 87.256599 49.607701 1015.510577
# 71 1.2 2 capsuleContent 20.0 98.191197 40.968947 1196.663873
# 72 1.2 2 capsuleContent 24.0 104.177736 26.424419 1331.450604
# 73 1.2 3 capsule 0.0 0.000000 0.00000 0.00000
# 74 1.2 3 capsule 1.0 2.528737 8.39300 4.19650
# 75 1.2 3 capsule 2.0 7.415708 22.57987 19.68293
# 76 1.2 3 capsule 3.0 15.845734 45.08950 53.51761
# 77 1.2 3 capsule 4.0 24.275759 62.14611 107.13542
# 78 1.2 3 capsule 5.0 33.133394 76.48998 176.45346
# 79 1.2 3 capsule 6.0 41.991029 87.35901 258.37796
# 80 1.2 3 capsule 6.5 45.900606 89.90799 302.69471
# 81 1.2 3 capsule 7.0 49.810183 92.12832 348.20378
# 82 1.2 3 capsule 7.5 53.719760 94.06237 394.75146
# 83 1.2 3 capsule 8.0 57.629337 95.74705 442.20381
# 84 1.2 3 capsule 8.0 57.629337 95.74705 442.20381
# 85 1.2 3 capsule 9.0 63.860225 93.23271 536.69369
# 86 1.2 3 capsule 10.0 70.091113 91.32747 628.97378
# 87 1.2 3 capsule 12.0 79.498532 79.70975 800.01101
# 88 1.2 3 capsule 16.0 90.372043 49.52751 1058.48553
# 89 1.2 3 capsule 20.0 101.245554 40.79704 1239.13462
# 90 1.2 3 capsule 24.0 107.354268 26.67212 1374.07293
# 91 1.2 3 capsuleContent 0.0 0.000000 0.000000 0.000000
# 92 1.2 3 capsuleContent 1.0 1.490256 4.946231 2.473115
# 93 1.2 3 capsuleContent 2.0 5.338746 16.521316 13.206889
# 94 1.2 3 capsuleContent 3.0 13.341161 39.079386 41.007240
# 95 1.2 3 capsuleContent 4.0 21.343576 56.172708 88.633287
# 96 1.2 3 capsuleContent 5.0 30.017950 71.355393 152.397337
# 97 1.2 3 capsuleContent 6.0 38.692324 82.860036 229.505052
# 98 1.2 3 capsuleContent 6.5 42.571357 85.881180 271.690356
# 99 1.2 3 capsuleContent 7.0 46.450390 88.512793 315.288850
# 100 1.2 3 capsuleContent 7.5 50.329423 90.805099 360.118323
# 101 1.2 3 capsuleContent 8.0 54.208457 92.801847 406.020060
# 102 1.2 3 capsuleContent 8.0 54.208457 92.801847 406.020060
# 103 1.2 3 capsuleContent 9.0 60.439345 91.000989 497.921478
# 104 1.2 3 capsuleContent 10.0 66.670233 89.636393 588.240168
# 105 1.2 3 capsuleContent 12.0 76.322001 79.472871 757.349432
# 106 1.2 3 capsuleContent 16.0 87.256599 49.607701 1015.510577
# 107 1.2 3 capsuleContent 20.0 98.191197 40.968947 1196.663873
# 108 1.2 3 capsuleContent 24.0 104.177736 26.424419 1331.450604