0

I am fairly new to R.

I have a data frame containing two columns with integer values. The first contains customerID's (i=1 to 60000), while the second holds product categories of purchased goods (j=1 to 14). I want to create a binary 60000x14 matrix (customerID's as rows, product categories as columns) with value 1 if customer(i) has bought product category j and value 0 if customer(i) did not buy product j.

The data frame has no empty values e.g. for customer 1, there are rows only for the j product categories bought (typically less than 14), while customer 1 may have several rows for the same category j. I have tried to create a matrix of zeros and wanted to use a loop or possibly some apply function to put in 1's, where relevant, but as yet unsuccessful. Hope someone can help :-)

Crusader
  • 31
  • 2
  • Hi Crusader, you only have two columns, so the third mentioned in this question would just be a column of ones. You may or may not need to add it depending on which of the solutions you prefer. – Aaron left Stack Overflow Jan 03 '17 at 14:06
  • Welcome to SO and R. I guess you want to reshape your data from long to wide format and then - if necessary - convert it from integer to binary (TRUE/FALSE). There are many posts on that topic. Consider `df <- data.frame(cID=c(1,2,1,1), pID=c(1:3, 3));reshape2::dcast(df, cID~pID, fun.aggregate = function(x)length(x)>=1)` as a starter. Side note: Please hover over the R tag - it asks for a minimal reproducible example. [Here's a guide](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610). – lukeA Jan 03 '17 at 14:08
  • Thanks a lot, I tried the reshape2, acast method, almost works, except I get cumulative values for the columns, where each customer bought more than 1 item, where I only want 1's..(I get like 2,3,4,5 etc) – Crusader Jan 03 '17 at 15:02
  • Great. Then just test if >1. Or use the fun.aggregate parameter to acast; I bet it even gave you a message that it was using length by default. – Aaron left Stack Overflow Jan 03 '17 at 15:16

0 Answers0