我有一个循环,它遍历R中的大量.tsv文件,并创建一个带有结果的输出文件。输出文件中的每一行依次对应于处理每个输入文件的结果。我需要回顾输入文件,找出输出文件中的每个结果对应的结果。因此,我希望输出文件(big_data(的行名是输入tsv文件的名称,我在循环中尝试过,但没有成功。下面是我的缩写循环,当我删除rownames行时,它会起作用。
files <- list.files(path =".", pattern = ".tsv")
files
datalist = list()
for(i in 1:length(files)) {
other_trait <- read.table(files[i])
coloc_res = coloc::coloc.abf(dataset1 = other_trait, dataset2 = dataset2,p12 = 1e-5)
coloc_results=matrix(ncol=6,nrow=1,0)
coloc_results[1,]=coloc_res$summary
write.csv(coloc_results, paste0("processed_", basename(files[i])))
datalist[[i]] = coloc_results
big_data = do.call(rbind, datalist)
colnames(big_data)=c("n_snps","H0","H1","H2","H3","H4")
rownames(big_data)= paste0(basename(files[i]))
write.csv(big_data, "results.csv")
}
我正在努力处理的行是rownames(big_data(=paste0等…
假设coloc_results
属于data.frame
类
#create list of files
files <- list.files(path =".", pattern = ".tsv")
#create list to bind results to
datalist = list()
#loop through files
for(i in 1:length(files)) {
#read table
other_trait <- read.table(files[i])
#desired analysis
coloc_res <- coloc::coloc.abf(dataset1 = other_trait, dataset2 = dataset2,p12 = 1e-5)
coloc_results <- matrix(ncol=6,nrow=1,0)
coloc_results[1,] <- coloc_res$summary
#write results of analysis to individual file
write.csv(coloc_results, paste0("processed_", basename(files[i])))
#add column containing information regarding the inputfile
coloc_results$inputfile <- basename(files[i])
#add results of analysis to list
datalist[[i]] = coloc_results
}
#merge list to one data.frame
big_data <- do.call(rbind, datalist)
#tid colnames
colnames(big_data) <- c("n_snps","H0","H1","H2","H3","H4", "inputfile)
#write to csv
write.csv(big_data, "results.csv")
请注意,do.call(rbind, datalist)
现在在for循环之外。因此,首先将所有项目添加到列表中,然后将整个列表转换为一个大数据帧。在您的原始代码中,您在每次迭代中都覆盖了results.csv
。