之前见到的矩阵都是稠密矩阵:如
myfiles <- list.files(pattern = "*matrices.csv")
myfiles
chow1<-read.csv(myfiles[1],sep=',',header=T,row.names = 1,stringsAsFactors = F)
chow1[1:10,1:5]
# AAACCTGAGAGTAATC AAACCTGGTAAATGTG AAACGGGAGAAACGAG AAACGGGAGGAGTACC AAACGGGCACCACCAG
#Sox17 1 0 0 1 0
#Mrpl15 0 0 0 0 0
#Lypla1 0 0 0 0 0
#Tcea1 0 0 0 0 1
#Atp6v1h 0 0 1 1 0
#Rb1cc1 0 0 0 0 0
#4732440D04Rik 0 0 0 0 0
#Pcmtd1 1 0 2 1 1
#Gm26901 0 0 0 0 0
#Rrs1 0 0 0 0 0
看看占了多少内存
format(object.size(chow1),units = "Mb")
#[1] "301.3 Mb"
转化为稀疏矩阵
chow1 <- as(as.matrix(chow1), "dgCMatrix")
chow1[1:10,1:5]
# AAACCTGAGAGTAATC AAACCTGGTAAATGTG AAACGGGAGAAACGAG AAACGGGAGGAGTACC AAACGGGCACCACCAG
#Sox17 1 . . 1 .
#Mrpl15 . . . . .
#Lypla1 . . . . .
#Tcea1 . . . . 1
#Atp6v1h . . 1 1 .
#Rb1cc1 . . . . .
#4732440D04Rik . . . . .
#Pcmtd1 1 . 2 1 1
#Gm26901 . . . . .
#Rrs1 . . . . .
看看占了多少内存
format(object.size(chow1),units = "Mb")
#[1] "58.8 Mb"
再转化为普通矩阵
chow1 <- as.matrix(chow1)
网友评论