首页 > 解决方案 > 从文本文件中读取特定列:R

问题描述

我正在尝试读取文本文件并创建一些特定列(大约 12 个)(位于特定长度)的数据框(称为数据集),如下所示:

  x <- fread("file1.txt",colClasses = "character", sep = "\n", header = FALSE, verbose = FALSE,strip.white = FALSE)
  y <- fread("file2.txt",colClasses = "character", sep = "\n", header = FALSE, verbose = FALSE,strip.white = FALSE)
  # combine them
  x = rbind(x,y)


  # We basically read the whole file as a string and then read substrings 
  # corresponding to each variable start and finish lengths.
  Var1= sapply(as.list(x$V1), stri_sub, from = 80, to = 82)
  Var1= as.data.frame(Var1)

  Var2= sapply(as.list(x$V1), stri_sub, 83, 89)
  Var2= as.data.frame(Var2)

  dataset = cbind(Var1,Var2)

运行两个分别有 200K 和 300K 行的文本文件大约需要 1 分钟。它们每行有 1800 个字符。有没有更快的方法来运行它?我将阅读大约 200 个这样的文件。

标签: rperformancefread

解决方案


我认为您可以通过以下方式简化代码

x <- Reduce(rbind, lapply(1:2, function(k) fread(paste0("file",k,".txt"),
                                                 colClasses = "character", 
                                                 sep = "\n", 
                                                 header = FALSE, 
                                                 verbose = FALSE,
                                                 strip.white = FALSE)))

dataset <- data.frame(Var1= substr(x$V1, 80, 82), Var2 = substr(x$V1,83,89))

当您substr在整个列上使用时,第二行可能会节省更多时间。


推荐阅读