I want to count unique values of categorical variable based of a column based on Date.
I want result as a matrix where column names are the values categorical variable, row names will be unique Date values and their cell values is the unique count.
The below links solves the group by problem but I am looking for the transformed df:
How to add count of unique values by group to R data.frame
R: Extract unique values in one column grouped by values in another column
My df has more than 50,000 rows and looks like:
dat <- data.frame(Date = c('06/08/2018','06/08/2018','07/08/2018','07/08/2018','08/08/2018','09/08/2018','09/08/2018','11/08/2018','11/08/2018','13/08/2018'),
Type= c('A','B','C','A','B','A','A','B','C','C'))
I want my resultant matrix to have "A", "B" ,"C" as new columns, "Date" as the rows and values in matrix as the unique count, shown in below image:
Also, it would be great that we don't hardcode categorical values. So, in future if instead of 3 it becomes 4, then code automatically handles it.