Starting from a data imported with
dati<- ( read.csv(file='C:...csv', header=TRUE, sep=";"))
I've chosen two variables
id<-dati$post_visid_low
item<-dati$event_list
than
id<-as.character(id)
item<-as.character(item)
dataT <- data.table(id, it
em)
The structure of dataT is
id item
1 102, 104, 108,401
2 405, 103, 650, 555, 450
3 305, 109
I want obtain this matrix of frequences with ordined columns
id 102 103 104 108 109 305 401 405 450 555 650
1 1 1 1
2 1 1 1 1
3 1 1
How can I do this? I tried with
library(Matrix)
id<-as.character(id)
item<-as.character(item)
dataT <- data.table(id, item)
lst <- strsplit(dataT$item, '\\s*,\\s*')
Un1 <- sort(unique(unlist(lst)))
sM <- sparseMatrix(rep(dataT$id, length(lst)),
match(unlist(lst), Un1), x= 1,
dimnames=list(dataT$id, Un1))
But i recevive this error
Error in i + (!(m.i || i1)) : non-numeric argument to binary operator
How I can do that?