将 txt 行拆分并分类为 table

Splitting and classifying txt lines into a table

我有一个很长的 word 文档,其中列出了如下项目:

  1. 项目 1
    • 条目 1
    • 条目 2
    • 条目 3
  2. 项目 2
    • 条目 1
    • 条目 2
    • 条目 3
  3. (等...)

项目是物种名称,条目是相应的位置和日期信息,但现在这些都无关紧要了。

我正试图将这个非常长的文档放入 R 中的一个合理的 table/tibble 对象中,我的想法是使用:

library (stringr)
data <- readLines("data.txt")
test_data <- str_sub(data, 1, 3)

然后用“数据”的每个元素的“项目”身份分配另一个向量(即每个日期+位置对应的物种)。我试图为此使用 for 循环并测试每一行是否以“”开头,但我被卡住了。

results <- vector (length = length(data))
   for (i in 1:length(data)) {
   if (test_data[i] != "   ") {
      results[i] = data[i]
   } else {
        while #here I am stuck

谢谢

我想我有一些事情可以开始了。这个想法是将您的文本文件加载为单个长字符串,然后将其分解为对应于 Item + 条目的片段并将其存储在列表中。最后,在列表中使用 lapply 来分隔 Item 和条目。

filename <- "test.txt"
# read your file a single long string
step1 <- readChar(filename, file.info(filename)$size)
# find the pattern that separate each Item (from a copy/paste of the example it is "\r\n\r\n") and make a list of items
# with associated entries
step2 <- as.list(unlist(strsplit(step1, split = "\r\n\r\n")))
# lastly split the vectors from step2
step3 <- lapply(step2, function(x) unlist(strsplit(x, split = "\r\n    ")))

输出:

> step3
[[1]]
[1] "Item 1" "entry1" "entry2" "entry3"

[[2]]
[1] "Item 2" "entry1" "entry2" "entry3"

从这里您可以开始使用“常规”工具来清理和组织数据例如

df <- as.data.frame(do.call(rbind, step3))
df <- tidyr::pivot_longer(df, 2:ncol(df))
df <- df[, -2]
names(df) <- c("Items", "Entries")
df
# A tibble: 6 x 2
  Items  Entries
  <chr>  <chr>  
1 Item 1 entry1 
2 Item 1 entry2 
3 Item 1 entry3 
4 Item 2 entry1 
5 Item 2 entry2 
6 Item 2 entry3 

这是一个基于“每个条目以十二个空格开头”这一事实的 tidyverse 方法。

# fake data
obj <- c("Item 1",
"            entry1",
"            entry2",
"            entry3",
"            entry4",
"Item 2",
"            entry1",
"            entry2",
"            entry3"
)

writeLines(obj, con = "data2.txt")


# read in and convert
library(tidyverse)

dat <- readLines("data.txt", skipNul = TRUE)

dat |>
  enframe() |>
  separate(
    value,
    into = c("item", "entry"),
    sep = "\s{12}",
    convert = TRUE,
    fill = "right"
  ) |>
  mutate(item = na_if(item, "")) |>
  fill(item, .direction = "down") |>
  filter(!(is.na(entry)))