Использование этого файла:
file_name
table1
Var1, Var2, Var3, Var4, Var5, Var6
198824, 198824, 198824, 198824, 198824, 198824
123, 1234, 1242, 124, 1241, 1232
file_name
table2
Var1, Var2, Var3, Var4, Var5, Var6
x, x, x, x, x, x
y, y, y, y, y, y
z, z, z, z, z, z
file_name
table3
Var1, Var2, Var3, Var4, Var5, Var6
532523, 25235, 532523, 25235, 532523, 25235
25332, 5325235, 25332, 5325235, 25332, 5325235
Сначала прочитайте все в виде символьных векторов.
library(readr)
library(stringr)
library(purrr)
library(dplyr)
# Could be done in base R, but {readr} will be faster on a large file
# read in all lines
lines_all <- read_lines("nested_tables.txt")
lines_all
#> [1] "file_name"
#> [2] "table1"
#> [3] "Var1, Var2, Var3, Var4, Var5, Var6"
#> [4] "198824, 198824, 198824, 198824, 198824, 198824"
#> [5] "123, 1234, 1242, 124, 1241, 1232"
#> [6] ""
#> [7] ""
#> [8] ""
#> [9] ""
#> [10] ""
#> [11] "file_name"
#> [12] "table2"
#> [13] "Var1, Var2, Var3, Var4, Var5, Var6"
#> [14] "x, x, x, x, x, x"
#> [15] "y, y, y, y, y, y"
#> [16] "z, z, z, z, z, z"
#> [17] ""
#> [18] ""
#> [19] ""
#> [20] ""
#> [21] ""
#> [22] "file_name"
#> [23] "table3"
#> [24] "Var1, Var2, Var3, Var4, Var5, Var6"
#> [25] "532523, 25235, 532523, 25235, 532523, 25235"
#> [26] "25332, 5325235, 25332, 5325235, 25332, 5325235"
Найдите в каждой строке совпадение с регулярным выражением для имени таблицы. Вам может потребоваться настроить соответствующий шаблон: "table[0-9]"
для соответствия вашим реальным именам.
# find where there's a string like "table1"
table_id_indices <- str_detect(lines_all, "table[0-9]")
table_id_indices
#> [1] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [12] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [23] TRUE FALSE FALSE FALSE
# extract the table names in order
table_id_names <- lines_all[table_id_indices]
table_id_names
#> [1] "table1" "table2" "table3"
Теперь, когда у вас есть вектор строк и индексы, с которых начинается каждый идентификатор, вы можете разделитьvector up.
# split the vector of lines into a list of vectors
# `cumsum` is a handy trick to "fill" from one TRUE value to the next
lines_chunked <- split(lines_all, cumsum(table_id_indices))
lines_chunked
#> $`0`
#> [1] "file_name"
#>
#> $`1`
#> [1] "table1"
#> [2] "Var1, Var2, Var3, Var4, Var5, Var6"
#> [3] "198824, 198824, 198824, 198824, 198824, 198824"
#> [4] "123, 1234, 1242, 124, 1241, 1232"
#> [5] ""
#> [6] ""
#> [7] ""
#> [8] ""
#> [9] ""
#> [10] "file_name"
#>
#> $`2`
#> [1] "table2"
#> [2] "Var1, Var2, Var3, Var4, Var5, Var6"
#> [3] "x, x, x, x, x, x"
#> [4] "y, y, y, y, y, y"
#> [5] "z, z, z, z, z, z"
#> [6] ""
#> [7] ""
#> [8] ""
#> [9] ""
#> [10] ""
#> [11] "file_name"
#>
#> $`3`
#> [1] "table3"
#> [2] "Var1, Var2, Var3, Var4, Var5, Var6"
#> [3] "532523, 25235, 532523, 25235, 532523, 25235"
#> [4] "25332, 5325235, 25332, 5325235, 25332, 5325235"
Чтобы сделать строки читаемыми, удалите все строки, не относящиеся к таблице.
# remove lines that don't have commas, since they're not tables
lines_chunked_cleaned <- map(lines_chunked, ~str_subset(.x, ",")) %>% compact()
lines_chunked_cleaned
#> $`1`
#> [1] "Var1, Var2, Var3, Var4, Var5, Var6"
#> [2] "198824, 198824, 198824, 198824, 198824, 198824"
#> [3] "123, 1234, 1242, 124, 1241, 1232"
#>
#> $`2`
#> [1] "Var1, Var2, Var3, Var4, Var5, Var6"
#> [2] "x, x, x, x, x, x"
#> [3] "y, y, y, y, y, y"
#> [4] "z, z, z, z, z, z"
#>
#> $`3`
#> [1] "Var1, Var2, Var3, Var4, Var5, Var6"
#> [2] "532523, 25235, 532523, 25235, 532523, 25235"
#> [3] "25332, 5325235, 25332, 5325235, 25332, 5325235"
Теперь каждый элемент списка можно прочитать как CSV.
# read in each vector of lines as a CSV
# forcing a default col_type prevents binding errors later
lines_chunked_csvs <- map(lines_chunked_cleaned, ~read_csv(.x, col_types = cols(.default = "c")))
lines_chunked_csvs
#> $`1`
#> # A tibble: 2 x 6
#> Var1 Var2 Var3 Var4 Var5 Var6
#> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 198824 198824 198824 198824 198824 198824
#> 2 123 1234 1242 124 1241 1232
#>
#> $`2`
#> # A tibble: 3 x 6
#> Var1 Var2 Var3 Var4 Var5 Var6
#> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 x x x x x x
#> 2 y y y y y y
#> 3 z z z z z z
#>
#> $`3`
#> # A tibble: 2 x 6
#> Var1 Var2 Var3 Var4 Var5 Var6
#> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 532523 25235 532523 25235 532523 25235
#> 2 25332 5325235 25332 5325235 25332 5325235
Используйте имена из ранее, чтобы идентифицировать каждый фрейм данных и связать их.
# name the list of tables, bind everything together
bind_rows(set_names(lines_chunked_csvs, table_id_names), .id = "table")
#> # A tibble: 7 x 7
#> table Var1 Var2 Var3 Var4 Var5 Var6
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 table1 198824 198824 198824 198824 198824 198824
#> 2 table1 123 1234 1242 124 1241 1232
#> 3 table2 x x x x x x
#> 4 table2 y y y y y y
#> 5 table2 z z z z z z
#> 6 table3 532523 25235 532523 25235 532523 25235
#> 7 table3 25332 5325235 25332 5325235 25332 5325235