I am using the following dataframe in R.
dput:
structure(list(uid = c("K-1", "K-1",
"K-2", "K-3", "K-4", "K-5",
"K-6", "K-7", "K-8", "K-9",
"K-10", "K-11", "K-12", "K-13",
"K-14"), Date = c("2020-03-16 12:11:33", "2020-03-16 12:11:33",
"2020-03-16 06:13:55", "2020-03-16 10:03:43", "2020-03-16 12:37:09",
"2020-03-16 06:40:24", "2020-03-16 09:46:45", "2020-03-16 12:07:44",
"2020-03-16 14:09:51", "2020-03-16 09:19:23", "2020-03-16 09:07:37",
"2020-03-16 11:48:34", "2020-03-16 06:23:24", "2020-03-16 04:39:03",
"2020-03-16 04:59:13"), batch_no = c(7, 7, 8, 9, 9, 8,
7, 6, 7, 9, 8, 8, 7, 7, 7), marking = c("S1", "S1", "S2",
"SE_hold1", "SD_hold1", "SD_hold2", "S3", "S3", "", "SA_hold3", "S1", "S1", "S2",
"S3", "S3"), seq = c("FRD",
"FHL", NA, NA, NA, NA, NA, NA, "ABC", NA, NA, NA, NA, "DEF", NA)), .Names = c("uid",
"Date", "batch_no", "marking",
"seq"), row.names = c(NA, 15L), class = "data.frame")
uid Date batch_no marking seq
K-1 16/03/2020 12:11:33 7 S1 FRD
K-1 16/03/2020 12:11:33 7 S1 FHL
K-2 16/03/2020 12:11:33 8 SE_hold1 ABC
K-3 16/03/2020 12:11:33 9 SD_hold2 DEF
K-4 16/03/2020 12:11:33 8 S1 XYZ
K-5 16/03/2020 12:11:33 NA ABC
K-6 16/03/2020 12:11:33 7 ZZZ
K-7 16/03/2020 12:11:33 NA S2 NA
K-8 16/03/2020 12:11:33 6 S3 FRD
- The
seq
column will have eight unique value includingNA
, not necessary the all 8 values are available for every day's date. batch_no
will have six unique values includingNA
and blank, not necessary the all six values are available for every day's date.- The
marking
column will have ~ 25 unique value but need to consider values with suffix_hold#
asHold
after that there would be six unique value including blank andNA
.
The requirement is to merge the dcast
dataframe in the following order to have a single view summary for an analysis.
I want to keep all the unique values static in the code, so that if the particular value is not available for a particular date I'll get 0 or - in summary table.
Desired Output:
seq count percentage Marking count Percentage batch_no count Percentage
FRD 1 12.50% S1 2 25.00% 6 1 12.50%
FHL 1 12.50% S2 1 12.50% 7 2 25.00%
ABC 2 25.00% S3 1 12.50% 8 2 25.00%
DEF 1 12.50% Hold 2 25.00% 9 1 12.50%
XYZ 1 12.50% NA 1 12.50% NA 1 12.50%
ZZZ 1 12.50% (Blank) 1 12.50% (Blank) 1 12.50%
FRD 1 12.50% - - - - - -
NA 1 12.50% - - - - - -
(Blank) 0 0.00% - - - - - -
Total 8 112.50% - 8 100.00% - 8 100.00%
For seq
we have % > 100 because of double counting of same uid
for value FRD
and FHL
. That is the accepted scenario. In Total
will have only distinct count of uid
.
I'm using below-mentioned code by SO, but couldn't get the desired output.
df = df_original %>%
mutate(marking = if_else(str_detect(marking,"hold"),"Hold", marking)) %>%
mutate_at(vars(c("seq", "batch_no", "marking")), forcats::fct_explicit_na, na_level = "(Blank)")
## You Need to do something similar with vectors of the possible values
df_combinations = purrr::cross_df(list(seq = df$seq %>% unique(),
batch_no = df$batch_no %>% unique(),
marking = df$marking %>% unique()))
df_all_combination = df_combinations %>%
left_join(df, by = c("seq", "batch_no", "marking")) %>%
group_by(seq, batch_no, marking) %>%
summarise(count = n())