We can do this with mtabulate
from qdapTools
library(qdapTools)
cbind(dat['id'], mtabulate(strsplit(dat$item, '\\s*,\\s*')))
# id 102 103 108 301 401 405 505 708
#1 1 1 1 0 0 1 0 0 0
#2 2 1 0 1 1 0 0 0 0
#3 3 0 1 1 0 0 1 1 1
NOTE: Data taken from @thelatemail's post.
Or another option (if we need a sparseMatrix
)
library(Matrix)
#split the 'item' column to `list`
lst <- strsplit(dat$item, '\\s*,\\s*')
#get the `unique` elements after `unlist`ing.
Un1 <- sort(unique(unlist(lst)))
#create a `sparseMatrix` by specifying the row
#column index along with dim names (if needed)
sM <- sparseMatrix(rep(dat$id, lengths(lst)),
match(unlist(lst), Un1), x= 1,
dimnames=list(dat$id, Un1))
sM
# 3 x 8 sparse Matrix of class "dgCMatrix"
# 102 103 108 301 401 405 505 708
#1 1 1 . . 1 . . .
#2 1 . 1 1 . . . .
#3 . 1 1 . . 1 1 1
It can be converted to matrix
by wrapping with as.matrix
as.matrix(sM)
# 102 103 108 301 401 405 505 708
#1 1 1 0 0 1 0 0 0
#2 1 0 1 1 0 0 0 0
#3 0 1 1 0 0 1 1 1