Contingency table based on third variable (numerical)

Some time ago I asked a question about creating data about a basket. Now I would like to create a similar data.frame file, but based on the third variable. Unfortunately, I run into problems. Previous question: An effective way to create a market basket matrix in R
@shadow and @ SimonO101 gave me good answers, but I could not change their anwser correctly. I have the following data:

Customer <- as.factor(c(1000001,1000001,1000001,1000001,1000001,1000001,1000002,1000002,1000002,1000003,1000003,1000003))
Product <- as.factor(c(100001,100001,100001,100004,100004,100002,100003,100003,100003,100002,100003,100008))
input <- data.frame(Customer,Product)

I can create an emergency table as follows:

input_df <- as.data.frame.matrix(table(input))

However, I have a third (numeric) variable that I want as output in the table.

Number <- c(3,1,-4,1,1,1,1,1,1,1,1,1) 
input <- data.frame(Customer,Product,Number)

(, 3 ) . , , . Number ( 0, ), :

input_agg <- aggregate( Number ~ Customer + Product, data = input, sum)

, , , - .

+4
2

xtabs :

R> xtabs(Number~Customer+Product, data=input)

         Product
Customer  100001 100002 100003 100004 100008
  1000001      0      1      0      2      0
  1000002      0      0      3      0      0
  1000003      0      1      1      0      1
+6

reshape2::dcast...

require( reshape2 )
#  Too many rows so change to a data.table.
dcast( input , Customer ~ Product , fun = sum , value.var = "Number" )
#  Customer 100001 100002 100003 100004 100008
#1  1000001      0      1      0      2      0
#2  1000002      0      0      3      0      0
#3  1000003      0      1      1      0      1

dcast data.table @Arun, FR # 2627. . 1.8.11. dcast.data.table. , dcast S3 reshape2. :

require(reshape2)
require(data.table)
input <- data.table(input)   
dcast.data.table(input , Customer ~ Product , fun = sum , value.var = "Number")
#    Customer 100001 100002 100003 100004 100008
# 1:  1000001      0      1      0      2      0
# 2:  1000002      0      0      3      0      0
# 3:  1000003      0      1      1      0      1

, reshape2:::dcast.


reshape:::cast, ... !

require(reshape)
input <- data.table( input )
cast( input , Customer ~ Product , fun = sum , value = .(Number) )
+4

All Articles