library
(Matrix)
buyer <-
data.frame
(Buyers =
c
(
"Robert"
,
"Stewart"
,
"Kristen"
,
"Joe"
,
"Kriti"
,
"Rafel"
))
buyer
car <-
data.frame
(Cars =
c
(
"Maruti"
,
"Sedan"
,
"SUV"
,
"Baleno"
,
"Hyundai"
,
"BMW"
,
"Audi"
))
car
order <-
data.frame
(Buyers =
c
(
"Robert"
,
"Robert"
,
"Stewart"
,
"Stewart"
,
"Kristen"
,
"Kristen"
,
"Joe"
,
"Kriti"
,
"Joe"
),
Cars =
c
(
"Maruti"
,
"Maruti"
,
"BMW"
,
"BMW"
,
"Audi"
,
"Audi"
,
"Maruti"
,
"Audi"
,
"Sedan"
))
order$RowIndex <-
match
(order$Buyers, buyer$Buyers)
order$ColIndex <-
match
(order$Cars, car$Cars)
order
msparse1 <-
sparseMatrix
( i = order$RowIndex, j = order$ColIndex)
msparse1
msparse2 <-
sparseMatrix
( i = order$RowIndex, j = order$ColIndex,
dims =
c
(
nrow
(buyer),
nrow
(car)),
dimnames =
list
(buyer$Buyers, car$Cars))
msparse2
msparse3 <-
sparseMatrix
( i = order$RowIndex, j = order$ColIndex, x = 1L,
dims =
c
(
nrow
(buyer),
nrow
(car)),
dimnames =
list
(buyer$Buyers, car$Cars))
msparse3