I have the following dataframe in R.
ID Date List Type
P-10012 2020-04-15 12:13:15 ABC,ABD,BCD TR1
P-10012 2020-04-15 12:13:15 ABC,ABD,BCD RES
P-10012 2020-04-15 12:13:15 ABC,ABD,BCD FTT
P-10013 2020-04-12 17:10:05 TR1
P-10013 2020-04-12 17:10:05 FTT
P-10013 2020-04-12 17:10:05 ZXR
P-10014 2020-04-10 04:30:19 ABD,BCD TR1
P-10014 2020-04-10 04:30:19 ABD,BCD ZXR
P-10015 2020-04-10 14:13:15 ABC
P-10016 2020-04-10 13:13:15
P-10017 2020-03-18 10:13:15 ABC,ABD,BCD TR1
dput(df)
df <- structure(list(ID = c("P-10012", "P-10012",
"P-10012", "P-10013", "P-10013", "P-10013",
"P-10014", "P-10014", "P-10015", "P-10016",
"P-10017"), Date = c("2020-04-15 12:13:15", "2020-04-15 12:13:15",
"2020-04-15 12:13:15", "2020-04-12 17:10:05", "2020-04-12 17:10:05",
"2020-04-12 17:10:05", "2020-04-10 04:30:19", "2020-04-10 04:30:19",
"2020-04-10 14:13:15", "2020-04-10 13:13:15", "2020-03-18 10:13:15"
), Type = c("TR1", "RES", "FTT", "TR1", "FTT", "ZXR", "TR1", "ZXR", NA, NA, "TR1"), List = c("ABC,ABD,BCD", "ABC,ABD,BCD", "ABC,ABD,BCD",
"", "", "", "ABD,BCD", "ABD,BCD", "ABC", "", "ABC,ABD,BCD")), class = "data.frame", row.names = c(NA,
-11L))
The structure of the dataframe is that it will always have the same List value for a particular ID in case if there are multiple row available for that particular ID because it has multiple different value in Type. If for a particular ID there is only 1 Type value then it will always have one row.
I need to create the following distribution for the month of Apr-20 of List values as mentioned in the comma separated manner and Type values.
Where, the first 7 rows in my Required Df are distinct count of ID based on the condition (i.e whether List or Type are blank or not) and distribution across all unique List and Type value. For these 7 rows the Distinct_Count should be divided by Total to get Percentage. However, from 8th row and onward if the unique value is form List then it should be divided by total distinct count of Non_Blank_List and if the value is from Type then it should be divided by total distinct count of Non_Blank_Type.
In the following matrix, I just want to understand what is the distribution of unique values of List and Type distinctly and in combination with other values.
Please not that for the example purpose I have simplified the List and Type values in 3 and 4 unique values respectively but in my actual dataframe it is quite high and it varies from month to month so please don't hard code the values.
I have tried multiple approach but couldn't achieve the required output yet.
Required Df<-
APR-21 Distinct_Count Percentage ABC ABD BCD TR1 RES FTT ZXR
Total_ID 5 100.00% 2 2 2 3 1 2 2
Blank_List 2 40.00% 0 0 0 1 0 1 1
Blank_Type 2 40.00% 1 0 0 0 0 0 0
Both_Blank 1 20.00% 0 0 0 0 0 0 0
Non_Blank_List 3 60.00% 2 2 2 2 1 1 1
Non_Blank_Type 3 60.00% 1 2 2 3 1 1 2
Both_Non_Blank 2 40.00% 1 2 2 2 1 1 1
ABC 1 33.33% 2 1 1 1 1 1 0
ABD 0 0.00% 1 2 2 2 1 1 1
BCD 0 0.00% 1 2 2 2 1 1 1
TR1 0 0.00% 1 2 2 3 1 1 1
RES 0 0.00% 1 1 1 1 1 1 0
FTT 0 0.00% 1 1 1 2 1 2 1
ZXR 0 0.00% 0 1 1 1 0 1 2