I have a df which has data like this:
sub = c("X001","X002", "X001","X003","X002","X001","X001","X003","X002","X003","X003","X002")
month = c("201506", "201507", "201506","201507","201507","201508", "201508","201507","201508","201508", "201508", "201508")
tech = c("mobile", "tablet", "PC","mobile","mobile","tablet", "PC","tablet","PC","PC", "mobile", "tablet")
brand = c("apple", "samsung", "dell","apple","samsung","apple", "samsung","dell","samsung","dell", "dell", "dell")
revenue = c(20, 15, 10,25,20,20, 17,9,14,12, 9, 11)
df = data.frame(sub, month, brand, tech, revenue)
I want to use sub and month as key and get one row for every subscriber per month which displays the sum of revenues for the unique values in tech and brand for that subscriber for that month. This example is simplified and with less columns as I have a huge data set I decided to try doing it with data.table
.
I have managed to do this for one catagorical column, either tech or brand using this:
df1 <- dcast(df, sub + month ~ tech, fun=sum, value.var = "revenue")
but I want to do it for two or more caqtogorical columns, so far I've tried this:
df2 <- dcast(df, sub + month ~ tech+brand, fun=sum, value.var = "revenue")
and it just concatenates the unique values of both catogorical columns and sums for that but I do not want that. I wan seperate columns for each unique value of all catogorical columns.
I'm new to R and would really appreciate any help.