R:如何在 R 中将多个 txt.gz 文件读入单个 table

R: How to read multiple txt.gz files into a single table in R

我一直在努力弄清楚如何在 R 中读取多个 txt.gz 文件并将它们合并为一个 table。我一直在处理的数据如下所示:

> head(case_file_ids_directory_OS)
                               case_id                              file_id
1 082f81eb-f43e-5e2a-bb4e-89380c9faf22 e2979e21-1e01-44c5-b13b-31ca3a5bae17
2 0a30a0cf-28b5-5342-850d-ea6fbe9aa356 eec04f88-7f32-4d0b-b376-e2730e40c661
3 0a507c33-d59d-594a-9fa3-8a2742e7c357 c8f37400-76e8-4386-be79-4e72c6255711
4 0b08b8c4-a0ce-5dd7-8386-609aac3a7f11 91868ced-9f2e-46f1-9a0e-0ba912fa7e6e
5 0bb51ed6-41bb-56a3-bd80-a297992b61c9 a0f31602-503b-4964-b68c-1ef996536a74
6 0e6764f4-12e5-50e0-8c63-163fdca0ab3a 766be8c6-98d6-4c7c-8fa8-824f48a67600
                                                                                                                                                           directory
1 C:\Users\ju1ce\AppData\Local/GenomicDataCommons/GenomicDataCommons/Cache/e2979e21-1e01-44c5-b13b-31ca3a5bae17/bf28e693-6b4a-4577-a8c8-88d073015bb6.FPKM.txt.gz
2 C:\Users\ju1ce\AppData\Local/GenomicDataCommons/GenomicDataCommons/Cache/eec04f88-7f32-4d0b-b376-e2730e40c661/6670f0a0-a322-4a63-b979-56b64d22d8e9.FPKM.txt.gz
3 C:\Users\ju1ce\AppData\Local/GenomicDataCommons/GenomicDataCommons/Cache/c8f37400-76e8-4386-be79-4e72c6255711/781a79f5-89e2-4ae3-9ad4-e040832beebd.FPKM.txt.gz
4 C:\Users\ju1ce\AppData\Local/GenomicDataCommons/GenomicDataCommons/Cache/91868ced-9f2e-46f1-9a0e-0ba912fa7e6e/81cc4c97-7c6d-4af6-a482-d5882694e787.FPKM.txt.gz
5 C:\Users\ju1ce\AppData\Local/GenomicDataCommons/GenomicDataCommons/Cache/a0f31602-503b-4964-b68c-1ef996536a74/b5514b40-5b8b-4faf-a202-49438c386032.FPKM.txt.gz
6 C:\Users\ju1ce\AppData\Local/GenomicDataCommons/GenomicDataCommons/Cache/766be8c6-98d6-4c7c-8fa8-824f48a67600/89721483-3a16-4ba5-a5f6-98eff5dd9b71.FPKM.txt.gz
  days_to_last_follow_up
1                    289
2                   2246
3                    468
4                   1412
5                    295
6                   1896

而且,我同时使用了 gzfileread.table 函数来读取单个文件:

> case_file_ids_directory_OS[1,3]
[1] "C:\Users\ju1ce\AppData\Local/GenomicDataCommons/GenomicDataCommons/Cache/e2979e21-1e01-44c5-b13b-31ca3a5bae17/bf28e693-6b4a-4577-a8c8-88d073015bb6.FPKM.txt.gz"
> a <- read.table(gzfile(case_file_ids_directory_OS[1,3]))
> colnames(a)[2] <- case_file_ids_directory_OS[1,1]
> colnames(a)[1] <- "gene_id"
> b <- read.table(gzfile(case_file_ids_directory_OS[2,3]))
> colnames(b)[2] <- case_file_ids_directory_OS[2,1]
> colnames(b)[1] <- "gene_id"
> cbind_ab <- merge(a,b,by="gene_id")

而且,我得到了以下 table:

> head(cbind_ab)
             gene_id 082f81eb-f43e-5e2a-bb4e-89380c9faf22 0a30a0cf-28b5-5342-850d-ea6fbe9aa356
1 ENSG00000000003.13                           0.02191867                           0.06367634
2  ENSG00000000005.5                           0.00000000                           0.00000000
3 ENSG00000000419.11                          33.29157249                          17.52750829
4 ENSG00000000457.12                           2.49297241                           2.09589637
5 ENSG00000000460.15                           2.25514246                           2.39028584
6 ENSG00000000938.11                          12.40726475                          32.46166434

哪些列名是“case_file_ids_directory_OS”中的“case_id”。

但是,我想对存储在 case_file_ids_directory_OS$directory 中的所有其他剩余文件重复这些步骤 我可以以“硬编码”方式手动执行这些操作,但我相信会有更有效、更简单的方法来执行这些操作,因为这些文件总共有 187 个。有没有最好的方法来读取 txt.gz 文件(称为完整目录)、合并它们并相应地更改 colnames?请指教

试试这个代码:

result <- Reduce(function(p, q) merge(p, q, by = 'gene_id'), Map(function(x, y) {
  data1 <- read.table(x)
  names(data1) <- c('gene_id', y)
  data1
}, case_file_ids_directory_OS$directory, case_file_ids_directory_OS$case_id))

result