I have a df that 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 a key and get one line for each subscriber per month, which displays the amount of income for unique values ββin the technology and the brand for this subscriber for this month. This example is simplified and with fewer columns, since I have a huge data set, I decided to try to do this using data.table .
I managed to do this for a single column column, be it a technique or a brand, using this:
df1 <- dcast(df, sub + month ~ tech, fun=sum, value.var = "revenue")
but I want to do this for two or more caqtogorical columns while I tried this:
df2 <- dcast(df, sub + month ~ tech+brand, fun=sum, value.var = "revenue")
and it just concatenates the unique values ββof both the column columns and the sum for that, but I don't want that. I separate separate columns for each unique value of all columns.
I am new to R and would really appreciate any help.