gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:36:59 30 4
gpt4 key购买 nike

问题

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

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

输入文件如下所示:

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

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

<表类="s-表"><头>X1X2<日>... Xn<正文>2021-01-01文本......2021-01-02文本..................

我的尝试

我试过了 base , data.table , readrvroom ,它们都有两个输出之一,或者是具有单列的数据框,或者是向量。我想避免 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")

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

提前致谢!

最佳答案

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

关于r - 是否有一个 R 函数可以读取带有\n 作为(列)分隔符的文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67060174/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com