Slightly modified example data.

cnt_mat <- structure(list(cell_id = c("sample08_cell813", "sample10_cell481",
"sample03_cell173", "sample07_cell140", "sample03_cell021", "sample08_cell091",
"sample08_cell091"), gene_id = c("TLA092", "TLA167", "TLA029",
"TLA358", "TLA314", "TLA228", "TLA029"), read_count = c(6, 10,
14, 10, 9, 8, 2)), row.names = c(NA, -7L), class = "data.frame")

> cnt_mat
           cell_id gene_id read_count
1 sample08_cell813  TLA092          6
2 sample10_cell481  TLA167         10
3 sample03_cell173  TLA029         14
4 sample07_cell140  TLA358         10
5 sample03_cell021  TLA314          9
6 sample08_cell091  TLA228          8
7 sample08_cell091  TLA029          2

You can construct the sparse matrix directly. This is assuming that there are no 0 counts in the long data (might want to double check to be sure).

library("Matrix")

sparse_mat <- sparseMatrix(
  i=match(cnt_mat$gene_id, unique(cnt_mat$gene_id)),
  j=match(cnt_mat$cell_id, unique(cnt_mat$cell_id)),
  x=cnt_mat$read_count,
  dims=c(length(unique(cnt_mat$gene_id)), length(unique(cnt_mat$cell_id))),
  dimnames=list(unique(cnt_mat$gene_id), unique(cnt_mat$cell_id))
)

> sparse_mat
6 x 6 sparse Matrix of class "dgCMatrix"
       sample08_cell813 sample10_cell481 sample03_cell173 sample07_cell140
TLA092                6                .                .                .
TLA167                .               10                .                .
TLA029                .                .               14                .
TLA358                .                .                .               10
TLA314                .                .                .                .
TLA228                .                .                .                .
       sample03_cell021 sample08_cell091
TLA092                .                .
TLA167                .                .
TLA029                .                2
TLA358                .                .
TLA314                9                .
TLA228                .                8



Source link