I initially thought this was a duplicate of Create Sparse Matrix from a data frame, but encountered errors relating to the requirement that assignment-indexing of sparseMatrices needs to be numeric and those queryid
and wifi
columns appear to be factors (or character). I'm going to assume they are factors, but users should check.
library(Matrix)
(M <- with( dat, sparseMatrix(i= as.numeric(queryid), j=as.numeric(wifi),x=rssi)))
#------
3 x 3 sparse Matrix of class "dgCMatrix"
[1,] 10 20 .
[2,] -10 . .
[3,] . . 15
dimnames(M) <- list( levels(dat$queryid), levels(dat$wifi) )
#-------
> M
3 x 3 sparse Matrix of class "dgCMatrix"
wifi1 wifi2 wifi3
0004920b 10 20 .
11000492 -10 . .
1114920b . . 15
It would actually be more difficult to accomplish if these were character columns. Thinking about it (but not testing), I'd probably use this code after creating factors for the character columns.