我々は、重複、このマーキングされていない場合は、私がお答えします。少しの前処理でこれを処理してから、テキストの連続したセクションを列挙するcumsum
、最後にread.table
でこれらのセクションを表として読み取ることができます。
サンプルデータを作成する:
file_text <- readLines(textConnection("
+ MODEL OUTPUT
+
+ Run: 1
+ V1 V2 V3
+ mm
+ 20 2 2.0
+ 21 2 1.5
+ 22 2 3.5
+
+ Run: 2
+ V1 V2 V3
+ mm
+ 1 1 1.5
+ 2 1 2.5
+
+ Run: 3
+ V1 V2 V3
+ mm
+ 11 5 1.5
+ 12 5 2.5
+ 13 5 1.0
+ 14 5 4.5"))
前処理:+
をリード、MODEL OUTPUT
をなくし、そしてのみmm
と行。
file_text = file_text[!grepl('MODEL OUTPUT', file_text)]
file_text = file_text[!grepl('Run: \\d+', file_text)]
file_text = sapply(file_text, sub, pattern = "^\\s*\\+", replacement = "")
file_text = file_text[!grepl('^\\s*mm\\s*$', file_text)]
空白行を特定します - セクション間の区切りと呼んで、セクションごとに番号を付けます。
is_break = unname(sapply(file_text, function(x) trimws(x) == ""))
section_id = unname(cumsum(is_break))
section_id
# [1] 1 2 2 2 2 2 3 3 3 3 4 4 4 4 4 4
は最後のセクションにファイルのテキストを分割し、テーブルなど、それらを読んで:
tabs = lapply(unique(section_id), function(i) {
# the first line of a section will always be empty
section_lines = file_text[section_id == i][-1]
if (length(section_lines)) {
# there's a section of text
read.table(text = section_lines, header = TRUE)
} else {
# there were two consecutive section breaks, so after the first break
# there's an empty 'section'
NA
}
})
結果はdata.framesのリストです。必要に応じて、欠落しているテーブルを今または以前に処理します。
tabs
# [[1]]
# [1] NA
#
# [[2]]
# V1 V2 V3
# 1 20 2 2.0
# 2 21 2 1.5
# 3 22 2 3.5
#
# [[3]]
# V1 V2 V3
# 1 1 1 1.5
# 2 2 1 2.5
#
# [[4]]
# V1 V2 V3
# 1 11 5 1.5
# 2 12 5 2.5
# 3 13 5 1.0
# 4 14 5 4.5
#
私は[私は各レコードが段落があるRにテキストファイルを読み込み、いくつかのレコードは4つのフィールドを持っているし、他の人が6を持っているにはどうすればよい]だと思います(http://stackoverflow.com/questions/8452473/how-各レコードが-n-paragraph-and-some-recordであるときのr-to-i-read-a-text-file-do)はあなたを動かすはずです。 – Henrik
関連:[* "Rのテキストファイルからのパターン間のデータを抽出" *](http://stackoverflow.com/questions/37663246/extract-data-between-a-pattern-from-a-text- file-in-r)と[* "非構造化csvファイルをデータフレームに変換する*"](http://stackoverflow.com/questions/33719058/r-convert-unstructured-csv-file-to-a-data -frame) – Jaap
いくつかの['readLines' - ' cumsum' - 'split'テーマ](http://stackoverflow.com/search?tab=votes&q=%5br%5d%20readLines%20cumsum%20split)です。あなたの詐欺を選ぶ;)幸運を! – Henrik