【问题标题】:Concatenate various .txt and .html files to one .txt file in R将各种 .txt 和 .html 文件连接到 R 中的一个 .txt 文件
【发布时间】:2020-04-29 10:06:58
【问题描述】:

我正在尝试编写一个脚本,该脚本自动将特定文件夹中的所有文件自动连接到一个 .txt 文件,但我遇到了问题,因为我试图在将它们写入 txt 之前将它们合并到一个大数据帧中文件,我收到错误,因为列名不匹配,所以我使用了 smartbind,但不是我收到“双行名称”的错误。

这是我的代码:

library(gtools)

dir<-"/Users/max/Desktop/NISAT_All/Regions"

subdir_list<-list.dirs(dir, recursive=F) 
subdir_list<-list.dirs(subdir_list,  recursive=F)
as.matrix(subdir_list)
subdirs_General <- subdir_list[ grepl("General", subdir_list) ]
as.matrix(subdirs_General)
subdir_list <- subdir_list[ !grepl("General", subdir_list) ]
subdir_list<-list.dirs(subdir_list,  recursive=F)
as.matrix(subdir_list)


for (subdir in subdir_list){

  setwd(subdir)

  subdir <-list.files(subdir, recursive=T)
  files <- subdir[ grepl("Armed Groups and Small Guns", subdir) ]
  files <- c(files, subdir[ grepl("Arms Embargoes", subdir) ])
  files <- c(files, subdir[ grepl("Black Market", subdir) ])
  files <- c(files, subdir[ grepl("Brokering", subdir) ])
  files <- c(files, subdir[ grepl("Landmines", subdir) ])
  files <- c(files, subdir[ grepl("MANPADS", subdir) ])
  files <- c(files, subdir[ grepl("Production", subdir) ])
  files <- c(files, subdir[ grepl("Stockpile Security and Destruction", subdir) ])
  files <- c(files, subdir[ grepl("UN Processes", subdir) ])
  files <- c(files, subdir[ grepl("United Nations", subdir) ])
  files <- c(files, subdir[ grepl("Weapons Collection and Amnesties", subdir) ])

  dataframe <- data.frame()

  for (file in files){

      df_temp <- read.delim(file)
      dataframe <- smartbind(dataframe, df_temp, sep="\n")

  }
  #then write your final file
  write.table(dataframe,"MergedFiles.txt",sep="\n", row.names = F, eol = "\r")
  rm(dataframe)

}

在图片上您可以看到文件通常的样子,我只是想将它们合并为一个

有人可以帮忙吗?

【问题讨论】:

  • 请不要发布错误图片(以及代码/数据,虽然这里不是一个因素):它不能被复制或搜索(SEO),它会破坏屏幕阅读器,它可能不会非常适合某些移动设备。参考:meta.stackoverflow.com/a/285557/3358272(和xkcd.com/2116)。请直接包含代码或数据(例如,dput(head(x))data.frame(...))。
  • (1) 向data.frame 迭代地添加行符合逻辑,但它的性能会可怕地扩展:每次连接行时,它都会生成帧的完整副本,意味着第一个文件被复制n 次(如果n 文件被加载)。 (2) 合并数据时行名不能重复,如果行名有意义,建议每帧保存为帧本身的列,然后用rownames(x) &lt;- NULL清除行名,然后执行连接。
  • 你显示的文件是一个文本文件,你使用read.delim而不是readLines有什么特殊原因吗?
  • 基本上,read.delim 用于读取表格数据,而不是文本文件。使用适当的函数(readLines,正如 r2evans 所写)。

标签: r concatenation


【解决方案1】:

假设我有两个文本文件要合并:

test1.txt

I'm not a pheasant plucker, I'm a pheasant plucker's son

test2.txt

and I'm only plucking pheasants til the pheasant plucker comes.

我只是指定要合并的文件的名称,并创建一个包含合并内容的空变量:

files_to_combine <- c(path.expand("~/test1.txt"), path.expand("~/test2.txt"))
combined <- ""

现在我只需使用readLines() 来获取内容并将它们粘贴在一起。

for (i in seq_along(files_to_combine))
{
  combined <- paste0(combined,  readLines(files_to_combine[i], "\n")
}

最后,我将结果写回磁盘,如下所示:

writeLines(combined, path.expand("~/test3.txt"))

现在我的主目录中有这个文件:

test3.txt

I'm not a pheasant plucker, I'm a pheasant plucker's son
and I'm only plucking pheasants til the pheasant plucker comes.

【讨论】:

  • 最好将它们全部读入(例如lapply)并将它们连接一次(例如c)然后迭代构建它。虽然使用简单的列表/向量并没有那么大的问题,但它在惯用上更好,并且与不迭代 rbinding 数据帧的逻辑相似(由于重复复制数据而执行得非常糟糕)。/
  • 例如,我想使用combined &lt;- sapply(path.expand(c("~/test1","~/test2")), readLines)(或c(lapply(...)),尽管在本例中这不是改进)。
  • 谢谢@r2evans,你当然是对的。我对这个答案的方法是使用 OP 熟悉的习语来展示解决方案的道路。如果速度是一个问题,那么思考为什么这种方法很慢以及如何加快速度是一个全新的练习。
  • @r2evans 即我的印象是,以适当的格式读取和写入数据,以及理解数据帧和字符向量之间的区别对于 OP 来说已经足够了。在这个阶段试图了解应用功能可能会要求太多?还是我太消极了?
  • Allan,虽然您的观点并非没有道理,但学习 R 的惯用形式也很重要。在 R 中,这通常(当然不总是)涉及将向量和列表作为一个整体进行操作。此外,识别已知次优方法(迭代连接)是在学习一门新语言的早期做的一件好事。因此,虽然“保持简单”当然是一个好目标,但我也认为鼓励惯用方法是合理的。
猜你喜欢
  • 1970-01-01
  • 2021-05-31
  • 2018-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-17
  • 1970-01-01
相关资源
最近更新 更多