在 r 中的 for() 循环中转置和重命名数据帧

transpose and rename dataframes in a for() loop in r

我有一个具有以下模式的文件列表:sampleid_samplename_counts.csv 表示计数矩阵,行中有细胞名,列中有基因。

我正在尝试从这些文件生成计数矩阵以加载到 Seurat 包中,该包需要列中的细胞名和行中的基因。

我成功获得了矩阵,其中 x 是所有 .csv 文件名的向量(我需要为对象创建一个新名称,因为我无法在循环中重新分配 "i" ):

for (i in x) {
  assign(paste0(i, ".counts"), t(read.table(i, sep = ",")))
  }

但是得到的矩阵没有colnames(换成了V1,V2,V3...) 每个矩阵的第 1 行包含单元名称。

以下是创建正确的列名:

colnames(file.counts) <- file.counts[1,]
file.counts <- file.counts[-1,]

但这在 for() 循环中不起作用。我如何在初始或另一个循环中实现它?

编辑:

这是经过简单 read.table 后原始 .csv 文件的样子:

structure(list(V1 = c(NA, 121270371765165, 121270372580596, 121270373898541, 
121270374395228, 121270374676403, 121270375926581, 121270376000796, 
121270376290589, 121270378289958, 121270378621156, 121347513957787, 
121347516024694, 121347517659934, 121347518125797, 121347519671644, 
121347519760734, 121347519921075, 121347520489203, 121896195804531
), V2 = c("DPM1", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", ""), V3 = c("SCYL3", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""
), V4 = c("FGR", "", "", "", "", "", "", "", "", "1.0", "", "", 
"", "", "", "", "", "", "", ""), V5 = c("CFH", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", ""), 
    V6 = c("FUCA2", "", "", "", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", ""), V7 = c("GCLC", "", "", "", 
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
    ""), V8 = c("NFYA", "", "", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", ""), V9 = c("NIPAL3", "", 
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
    "", "", ""), V10 = c("LAS1L", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "", "", "", "")), row.names = c(NA, 
20L), class = "data.frame")

这是转置 t()

后的样子
structure(c(NA, "DPM1", "SCYL3", "FGR", "CFH", "FUCA2", "GCLC", 
"NFYA", "NIPAL3", "LAS1L", "ENPP4", "ANKIB1", "KRIT1", "RAD52", 
"BAD", "LAP3", "CD99", "MAD1L1", "LASP1", "SNX11", "1.212704e+14", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "1.212704e+14", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "1.0", "", "", "", "1.212704e+14", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "1.212704e+14", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "1.212704e+14", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "1.0", 
"", "1.212704e+14", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "1.212704e+14", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "1.212704e+14", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "1.212704e+14", "", "", "1.0", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", ""), .Dim = c(20L, 10L
), .Dimnames = list(c("V1", "V2", "V3", "V4", "V5", "V6", "V7", 
"V8", "V9", "V10", "V11", "V12", "V13", "V14", "V15", "V16", 
"V17", "V18", "V19", "V20"), NULL))

也许这有助于...

library( data.table )

#build list of csv.gz files (I only kept two files in the dir)
files.to.read <- list.files( "./temp/GSE114725_RAW/", pattern = "\.csv.gz$", full.names = TRUE )

#build a list of read data
L <- lapply( files.to.read, data.table::fread )

#transpose each list element
L.transposed <- lapply( L, data.table::transpose, keep.names = "gene", make.names = "V1" )
#set list names based on filenames
names(L.transposed) <- basename( files.to.read )

#write list elements to individual data.tables
for(i in 1:length(L.transposed)) assign(names(L.transposed)[i], L.transposed[[i]])