Reputation: 119
This is sample of data I am working with:
Here is the data splited into different files, one for 2 elements by group and anothe one with 3 elements by group
https://drive.google.com/drive/folders/13NcZ1eDRz7RfPIV0w-bOsF6kS1ePSQ9j?usp=sharing
In the column inspection I want to assing this codes by group (group variables are Code, Area, Floor and Box).
For each group combination:
If result=1 in one the cases and the other has result=4, inspection=1 to case with result=4 and inspection=2 to result=1
If result=1 in one the cases and the other has result=3, inspection=1 to case with result=3 and inspection=2 to result=1
If result=1 in one the cases and the other has result=1, inspection=1 one of the cases and to the other inspection=2.
How can I make this adjustment to inspection variable by each case groups to get the result in this image?
Then, I have cases with 3 elements per group:
For each group combination:
If result=1 in one the cases and the other two has result=4 and result=4, inspection=1 to first case with result=4, the second case with result=4 asign inspection=0 and inspection=2 to result=1
If result=1 in one the cases and the other two has result=4 and result=0, inspection=1 to the case with result=4, inspection=0 to case with result=0 and inspection=2 to result=1
If result=1 in all the cases, inspection=1 in one of the cases and to the other inspection=2, as we can see on the image below
How can I do that asignation?
Upvotes: 0
Views: 72
Reputation: 24722
How about just using the order of Result
to rearrange c(2,1)
, like this:
df %>%
group_by(Code, Area, Floor, Box) %>%
mutate(Inspection = c(2,1)[order(Result)])
Output:
Code Area Floor Box Result Inspection
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 10101 11 108 1 4 1
2 10101 11 108 1 1 2
3 10101 11 89 1 1 2
4 10101 11 89 1 1 1
5 10101 11 90 1 1 2
6 10101 11 90 1 3 1
Upvotes: 1