Based on the given link, are you looking for something like this?
library(data.table)
library(qdapTools)
crossprod(
as.matrix(
mtabulate(
dcast(setDT(DT), rowid(CRMPrivatkundeID) ~ CRMPrivatkundeID)[, -1]
)
)
)
# Esquinzo Playa Fleesensee Jandia Playa Masmavi Noblis Playa Granda Quinta die Ria
#Esquinzo Playa 1 1 0 0 0 0 0
#Fleesensee 1 14 0 1 0 0 0
#Jandia Playa 0 0 1 0 1 0 0
#Masmavi 0 1 0 1 0 0 0
#Noblis 0 0 1 0 1 0 0
#Playa Granda 0 0 0 0 0 1 1
#Quinta die Ria 0 0 0 0 0 1 1
This also works without mtabulate()
:
library(data.table)
crossprod(
as.matrix(
dcast(DT, CRMPrivatkundeID ~ Basket)[, -1]
)
)
Data
library(data.table)
f <- "Fleesensee"
DT <- data.table(
CRMPrivatkundeID = rep(c(56, 172, 240, 306, 365, 423, 427), each = 2L),
Basket = c(f, "Masmavi", f, f, f, "Esquinzo Playa", "Jandia Playa",
"Noblis", "Quinta die Ria", "Playa Granda", rep(f, 4))
)