是否有一个 R 函数可以读取以 \n 作为(列)分隔符的文本文件?

Is there an R function that reads text files with \n as a (column) delimiter?

问题

我正在尝试想出一种 neat/fast 方法来将由换行符 (\n) 分隔的文件读取到多个列中。

基本上在给定的输入文件中,输入文件中的多行在输出中应该变成单行,但是大多数文件读取函数明智地将换行符解释为表示新行,因此它们最终作为具有单列的数据框。这是一个例子:

输入文件如下所示:

Header Info
2021-01-01
text
...
@
2021-01-02
text
...
@
...

其中 ... 代表输入文件中可能存在的多行,而 @ 表示输出数据框中一行的结尾。所以在读取这个文件时,它应该变成这样的数据框(忽略header):

X1 X2 ... Xn
2021-01-01 text ... ...
2021-01-02 text ... ...
... ... ... ...

我的尝试

我试过 basedata.tablereadrvroom,它们都有两个输出之一,或者是具有单列的数据框, 或者一个向量。我想避免 for 循环,所以我目前的解决方案是使用 base::readLines(),将其作为字符向量读取,然后手动添加一些“适当的”列分隔符(例如 ;),然后加入又分裂了。

# Save the example data to use as input
writeLines(c("Header Info", "2021-01-01", "text", "@", "2021-01-02", "text", "@"), "input.txt")

input <- readLines("input.txt")
input <- paste(input[2:length(input)], collapse = ";") # Skip the header
input <- gsub(";@;*", replacement = "\n", x = input)
input <- strsplit(unlist(strsplit(input, "\n")), ";")
input <- do.call(rbind.data.frame, input)

# Clean up the example input
unlink("input.txt")

我上面的代码有效并给出了预期的结果,但肯定有更好的方法?? 编辑:这是函数内部的,所以任何简化的部分(也许是大部分)目的是提高速度。

提前致谢!

您可以通过以下方式绕过一些字符串操作:

input <- readLines("input.txt")[-1] #Read in and remove header
ncol <- which(input=="@")[1]-1  #Number of columns of data
data.frame(matrix(input[input != "@"], ncol = ncol, byrow=TRUE)) #Convert to dataframe 
#          X1   X2
#1 2021-01-01 text
#2 2021-01-02 text

1) 读入数据,找到给逻辑变量 at 的 @ 符号,然后创建一个分组变量 g,它对每个所需的行都有不同的值。最后使用 tapply with paste 将其重新加工成可以使用 read.table 读取的行并读取它。 (如果数据中有逗号,则使用其他分隔符。)

L <- readLines("input.txt")[-1]
at <- grepl("@", L)
g <- cumsum(at)
read.table(text = tapply(L[!at], g[!at], paste, collapse = ","), 
  sep = ",", col.names = cnames)

给这个数据框:

          V1   V2
1 2021-01-01 text
2 2021-01-02 text

2) 另一种方法是将数据重新处理为 dcf 格式,方法是删除 @ 符号并在其他行前面加上列名和冒号。然后使用read.dcf。 cnames 是您要使用的列名称的字符向量。

cnames <- c("Date", "Text")

L <- readLines("input.txt")[-1]
LL <- sub("@", "", paste0(c(paste0(cnames, ": "), ""), L))
DF <- as.data.frame(read.dcf(textConnection(LL)))
DF[] <- lapply(DF, type.convert, as.is = TRUE)
DF

给这个数据框:

        Date Text
1 2021-01-01 text
2 2021-01-02 text

3) 这种方法只是将数据重新整形为矩阵,然后将其转换为数据框。请注意,(1) 将数字列转换为数字列,而这个只是将它们保留为字符。

L <- readLines("input.txt")[-1]
k <- grep("@", L)[1]
as.data.frame(matrix(L, ncol = k, byrow = TRUE))[, -k]
##           V1   V2
## 1 2021-01-01 text
## 2 2021-01-02 text

基准

问题没有提到速度作为考虑因素,但在评论中后来提到了。根据以下基准测试中的数据,(1) 运行速度是问题代码的两倍,(3) 运行速度快近 25 倍。

library(microbenchmark)

writeLines(c("Header Info", 
   rep(c("2021-01-01", "text", "@", "2021-01-02", "text", "@"), 10000)), 
   "input.txt")

library(microbenchmark)
writeLines(c("Header Info", rep(c("2021-01-01", "text", "@", "2021-01-02", "text", "@"), 10000)), "input.txt")

microbenchmark(times = 10,
ques = {
  input <- readLines("input.txt")
  input <- paste(input[2:length(input)], collapse = ";") # Skip the header
  input <- gsub(";@;*", replacement = "\n", x = input)
  input <- strsplit(unlist(strsplit(input, "\n")), ";")
  input <- do.call(rbind.data.frame, input)
},
ans1 = {
  L <- readLines("input.txt")[-1]
  at <- grepl("@", L)
  g <- cumsum(at)
  read.table(text = tapply(L[!at], g[!at], paste, collapse = ","), sep = ",")
},
ans3 = {
  L <- readLines("input.txt")[-1]
  k <- grep("@", L)[1]
  as.data.frame(matrix(L, ncol = k, byrow = TRUE))[, -k]
})
## Unit: milliseconds
##  expr     min      lq    mean  median      uq     max neval cld
##  ques 1146.62 1179.65 1188.74 1194.78 1200.11 1219.01    10   c
##  ans1  518.95  522.75  548.33  532.59  561.55  647.14    10  b 
##  ans3   50.47   51.19   51.68   51.69   52.25   52.52    10 a  

在这一点上,您可能会考虑全力以赴并使用适当的语法来解析它。我不知道实际情况有多大或有多复杂,但使用 pegr 它可能看起来像这样:


input <-
"Header Info
2021-01-01
text
multiple lines
of
text
@
2021-01-02
text
more
lines of text
@
"

library(pegr)
peg <- new.parser(commonRules,action=TRUE) +
    c("HEADER   <- 'Header Info' EOL" , "{}"  ) + # Rule to match literal 'Header Info' and a \n, then discard
    c("TYPE     <- 'text' EOL"        , "{-}" ) + # Rule to match literal 'text', store paste and store as $TYPE
    c("DATE     <- (!EOL .)* EOL"     , "{-}" ) + # Rule to match any character leading up to a new line. Could improve to look for a date format
    c("EOS      <- '@' EOL"           , "{}"  ) + # Rule to match end of section, then discard
    c("BODY     <- (!EOS .)*"         , "{-}" ) + # Rule to match body of text, including newlines
    c("SECTION  <- DATE TYPE BODY EOS"        ) + # Combining rules to match each section
    c("DOCUMENT <- HEADER SECTION*"           )   # Combining more rules to match the endire document

res <- peg[["DOCUMENT"]](input))

final <- matrix( value(res), ncol=3, byrow=TRUE ) %>%
    as.data.frame %>%
    setnames( names(value(res))[1:3])

final

产生:

         DATE TYPE                       BODY
 1 2021-01-01 text multiple lines\nof\ntext\n
 2 2021-01-02 text      more\nlines of text\n

如果您不了解语法,可能会感觉笨拙,但一旦您了解,它就是一个即刻即弃的解决方案。它会 运行 根据规范,直到规范不成立。不用担心脆弱的预处理,也很容易适应未来不断变化的格式。

还有 tidyverse 方法:

library(tidyr)
library(readr)
library(stringr)

max_columns <- 5

d <- {
  readr::read_file("file.txt") %>% 
  stringr::str_remove("^Header Info\n") %>% 
  tibble::enframe(name = NULL) %>% 
  separate_rows(value, sep = "@\n")  %>% 
  separate("value", into = paste0("X", 1:max_columns) , sep = "\n") 
}

在名为 file.txt 的文件中使用您的示例输入,d 看起来像:

# A tibble: 3 x 5
  X1         X2    X3    X4    X5   
  <chr>      <chr> <chr> <chr> <chr>
1 2021-01-01 text  ...   ""    NA   
2 2021-01-02 text  ...   ""    NA   
3 ...        NA    NA     NA   NA   
Warning message:
Expected 5 pieces. Missing pieces filled with `NA` in 3 rows [1, 2, 3]. 

请注意,警告只是为了确保您知道自己得到了 NA,如果 @ 之间的行数不同,这是不可避免的

我使用的数据与提供的数据类似,用于演示。你也可以做这样的事情在结果数据框中有可变数量的列

example <- "Header Info
2021-01-01
text
multiple lines
of
text
@
2021-01-02
text
more
lines of text
@"
library(tidyverse)

example %>% as.data.frame() %>% setNames('dummy') %>%
  separate_rows(dummy, sep = '\n') %>%
  filter(row_number() !=1) %>%
  group_by(rowid = rev(cumsum(rev(dummy == '@')))) %>%
  filter(dummy != '@') %>%
  mutate(name = paste0('X', row_number())) %>%
  pivot_wider(id_cols = rowid, names_from = name, values_from = dummy)
#> # A tibble: 2 x 6
#> # Groups:   rowid [2]
#>   rowid X1         X2    X3             X4            X5   
#>   <int> <chr>      <chr> <chr>          <chr>         <chr>
#> 1     2 2021-01-01 text  multiple lines of            text 
#> 2     1 2021-01-02 text  more           lines of text <NA>

reprex package (v2.0.0)

于 2021-05-30 创建