mat <- data.frame(cell.1=c(2,6,1,8,1),cell.2=c(4,4,2,9,1),cell.3=c(3,3,3,0,2),row.names = paste0("gene.",seq(5))) %>% as.matrix()
> mat
cell.1 cell.2 cell.3
gene.1 2 4 3
gene.2 6 4 3
gene.3 1 2 3
gene.4 8 9 0
gene.5 1 1 2
矩阵与 矩阵列数相等的向量作整除
col_vector <- c(2,4,2)
mat_sweep(x = mat,MARGIN = 2,STATS = col_vector,FUN = "%/%") ## %/% 整除符号
cell.1 cell.2 cell.3
gene.1 1 1 1
gene.2 3 1 1
gene.3 0 0 1
gene.4 4 2 0
gene.5 0 0 1
矩阵与 矩阵行数相等的向量作除法
row_vectors <- c(2,3,1,2,3)
sweep(x = mat,MARGIN = 1,STATS = row_vectors,FUN = "/")
cell.1 cell.2 cell.3
gene.1 1.0000000 2.0000000 1.5000000
gene.2 2.0000000 1.3333333 1.0000000
gene.3 1.0000000 2.0000000 3.0000000
gene.4 4.0000000 4.5000000 0.0000000
gene.5 0.3333333 0.3333333 0.6666667
网友评论