I am attempting to convert data of the form
receipts <- data.frame(receipt=c(193, 193, 193, 987, 215, 215),
product=c("spanner", "saw", "widget", "glue", "widget", "hammer"),
quantity=c(3,1,50,1,2,5),
stringsAsFactors = FALSE)
# i.e.
# receipt product quantity
# 193 spanner 3
# 193 saw 1
# 193 widget 50
# 987 glue 1
# 215 widget 2
# 215 hammer 5
Into data of the form
product_matrix <- data.frame(receipt=c(193,987,215),
spanner=c(3,0,0),
saw=c(1,0,0),
widget=c(50,0,2),
glue=c(0,1,0),
hammer=c(0,0,5),
stringsAsFactors = FALSE)
# i.e
# receipt spanner saw widget glue hammer
# 193 3 1 50 0 0
# 987 0 0 0 1 0
# 215 0 0 2 0 5
Is there a quick and easy way to do this? (I was working on a custom function, but it was taking a lot longer than I thought it would, and figured this is probably a reasonably common task for which a function may already exist)