I have a dataframe like this
ID <- c("A","A","A","A","A","A","A","A")
Step <- c("Step_1","Step_1","Step_2","Step_2","Step_3","Step_3","Step_3","Step_4")
Passfail <- c("Pass","Pass","Fail","Pass","Fail","Fail","Pass","Fail")
Measurement <- c("Length","Length","Breadth","Breadth",
"Height","Height","Height","Width")
df <- data.frame(ID,Step,Passfail,Measurement)
I am trying to create several columns that when true should return 1 else 0 GROUPED by (Measurement,ID,ToolID)
For each group,
- AP = 1 if only passes in the passfail
- AF = 1 if only fails in the passfail
- SFP = 1 if there is only 1 fail and atleast 1 pass in passfail
- MFP = 1 if there are more than 1 fails and atleast 1 pass in passfail.
Desired Output is
Measurement ID Step AP AF SFP MFP
Length A Step_1 1 0 0 0
Breadth A Step_2 0 0 1 0
Height A Step_3 0 0 0 1
Width A Step_4 0 1 0 0
I am trying it this way to get the AP & AF columns but not quite getting it right
library(dplyr)
df1 <- df %>%
group_by(Measurement,ID,Step) %>%
mutate(AP = case_when((Passfail == "Pass" & Passfail != "Fail") ~ 1, TRUE ~ 0),
AF = case_when((Passfail == "Fail" & Passfail != "Pass") ~ 1, TRUE ~ 0)
) %>%
distinct()