【问题标题】:Combine and transpose many fixed-format dataset files quickly快速组合和转置许多固定格式的数据集文件
【发布时间】:2015-09-11 09:44:40
【问题描述】:

我拥有:~100 个 txt 文件,每个文件有 9 列和 >100,000 行 我想要的:一个组合文件,只有 2 列但所有行。那么这应该转置为 >100,000 列和 2 行的输出。

我创建了下面的函数来系统地浏览文件夹中的文件,拉出我想要的数据,然后在每个文件之后,与原始模板连接在一起。

问题:这在我的小测试文件上运行良好,但是当我尝试在大文件上执行此操作时,我遇到了内存分配问题。我的 8GB 内存还不够,我认为这部分是我编写代码的方式。

我的问题:有没有办法遍历文件,然后在最后一次全部加入以节省处理时间?

另外,如果放这种东西的地方不对,有什么更好的论坛来获取 WIP 代码的输入??

##Script to pull in genotype txt files, transpose them, delete commented rows & 
## & header rows, and then put files together.

library(plyr)

## Define function
Process_Combine_Genotype_Files <- function(
        inputdirectory = "Rdocs/test", outputdirectory = "Rdocs/test", 
        template = "Rdocs/test/template.txt",
        filetype = ".txt", vars = ""
        ){

## List the files in the directory & put together their path
        filenames <- list.files(path = inputdirectory, pattern = "*.txt")
        path <- paste(inputdirectory,filenames, sep="/")


        combined_data <- read.table(template,header=TRUE, sep="\t")

## for-loop: for every file in directory, do the following
        for (file in path){

## Read genotype txt file as a data.frame
                currentfilename  <- deparse(substitute(file))
                currentfilename  <- strsplit(file, "/")
                currentfilename <- lapply(currentfilename,tail,1)

                data  <- read.table(file, header=TRUE, sep="\t", fill=TRUE)

                #subset just the first two columns (Probe ID & Call Codes)
                #will need to modify this for Genotype calls....
                data.calls  <- data[,1:2]

                #Change column names & row names
                colnames(data.calls)  <- c("Probe.ID", currentfilename)
                row.names(data.calls) <- data[,1]


## Join file to previous data.frame
                combined_data <- join(combined_data,data.calls,type="full")


## End for loop
        }
## Merge all files
        combined_transcribed_data  <- t(combined_data)
print(combined_transcribed_data[-1,-1])
        outputfile  <- paste(outputdirectory,"Genotypes_combined.txt", sep="/")        
        write.table(combined_transcribed_data[-1,-1],outputfile, sep="\t")

## End function
}

提前致谢。

【问题讨论】:

  • 这可以通过freaddata.table 包轻松处理。

标签: r fread read.csv fixed-format


【解决方案1】:

试试:

filenames <- list.files(path = inputdirectory, pattern = "*.txt")
require(data.table)
data_list <- lapply(filenames,fread, select = c(columns you want to keep))

现在您有了所有数据的列表。假设所有 txt 文件确实具有相同的列结构,您可以通过以下方式组合它们:

data <- rbindlist(data_list)

转置数据:

t(data)

(感谢@Jakob H for select in fread)

【讨论】:

  • 我建议您在读取数据时删除多余的列以减少占用空间:data_list
  • 这对我来说效果很好。不幸的是,我的 write.table 命令使 R Studio 崩溃,我认为是因为我的组合文件太大了。谢谢!
  • @DataMunger 我的文件在其列标题中有空格(“Probe Set ID”而不是“Probe_Set_ID”。因此,我没有运气导入某些列。不确定是否有办法在这种情况下使用你的建议。我错过了什么吗?
  • 为什么不删除标题?如果您在加载数据时不删除不必要的列,您将耗尽内存。还有一种更直接的阅读选择列的方法是lapply(filennames, function(x) fread(x, select = c(columns needed)
  • 我遇到的问题是,当我只加载我需要的 1 行时,列/行名称正在消失。我认为这是因为它不再是 data.frame,所以也许使用 as.data.frame,我可以修复它...?
【解决方案2】:

如果速度/工作内存是问题,那么我建议使用 Unix 进行合并。一般来说,Unix 比 R 更快。此外,Unix 不需要将所有信息都加载到 RAM 中,而是以块的形式读取信息。因此,Unix 永远不会受到内存限制。如果您不了解 Unix,但计划将来经常操作大文件,那么请学习 Unix。它简单易学且功能强大。我将以 csv 文件为例。

在 R 中生成 CSV 文件

for (i in 1:10){
  write.csv(matrix(rpois(1e5*10,1),1e5,10), paste0('test',i,'.csv'))
}

在 Shell(即在 Mac 上)/Terminal(即在 Linux Box 上)/Cygwin(即在 Windows 上)

cut -f 2,3 -d , test1.csv > final.csv #obtain column 2 and 3 form test1.csv
cut -f 2,3 -d , test[2,9].csv test10.csv | sed 1d >> final.csv #removing header in test2.csv onward 

请注意,如果您安装了 Rtools,那么您可以使用 system 函数从 R 运行所有这些 Unix 命令。

将读取的 final.csv 转置为 R 并转置。

更新:

我对上面的代码进行了计时。运行时间 .4 秒。因此,要对 100 个文件而不是 10 个文件执行此操作,可能需要 4 秒。我没有对 R 代码计时,但是,当只有 10 个文件时,Unix 和 R 程序可能具有相似的性能,但是,如果有 100+ 个文件,您的计算机可能会受到内存限制并且 R 可能会崩溃.

【讨论】:

  • 你完全正确。我一直在努力在 Windows 上使用 Gywin 和在我的 Ubuntu 引导上使用终端来改进这一点。我知道这是题外话,但是有什么好的网站/教程来学习这些类型的计算命令吗?我正在通过 Datacamp 和 Coursera 等地方学习 R & Python。谢谢。
  • @GaiusAugustus 首先,对于数据争论,没有必要了解很多 Unix。例如,这不像学习 R。有了这个想法,这里有一些很好的博客文章bconnelly.net/working-with-csvs-on-the-command-line & practical-data-science.blogspot.com/2012/09/…
猜你喜欢
  • 2010-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-21
  • 2016-04-08
  • 2015-06-18
相关资源
最近更新 更多