I have such dataset
mydata=structure(list(SAP = c("S985", "S985", "S985", "S985", "S985",
"S985", "S985", "S985"), code = c(50000690L, 50000690L, 50000690L,
50000690L, 801L, 50000690L, 50000690L, 50000690L), dt_start = c("18.01.2022 8:00",
"18.01.2022 12:00", "18.01.2022 12:00", "18.01.2022 10:00", "22.01.2022 10:00",
"22.01.2022 10:00", "22.01.2022 12:00", "22.01.2022 12:00"),
dt_end = c("18.01.2022 10:00", "18.01.2022 22:00", "18.01.2022 22:00",
"18.01.2022 20:00", "22.01.2022 20:00", "22.01.2022 20:00",
"22.01.2022 22:00", "22.01.2022 22:00"), person_number = c(938541L,
938541L, 938541L, 938541L, 754105L, 867801L, 865394L, 938541L
)), class = "data.frame", row.names = c(NA, -8L))
In this dataset, we see that there is column person_number=938541 who have only one date 18.01 in the columns dt start and dt end. This means that on this date he works alone (do not pay attention to duplicates in the date, this is not important now). But in general, we see that whe have 4 unique person_number (persons).
754105
867801
865394
938541
How to do that if it so happened that on some date one person works, then we are looking for a all unique personal_number, and we sign randomly anyone on the date where work just one personal_number. For example desired result like this
SAP code dt_start dt_end person_number
S985 50000690 18.01.2022 8:00 18.01.2022 10:00 938541
S985 50000690 18.01.2022 12:00 18.01.2022 22:00 938541
S985 50000690 18.01.2022 12:00 18.01.2022 22:00 938541
S985 50000690 18.01.2022 10:00 18.01.2022 20:00 938541
**S985 50000690 18.01.2022 12:00 18.01.2022 22:00 865394**
S985 801 22.01.2022 10:00 22.01.2022 20:00 754105
S985 50000690 22.01.2022 10:00 22.01.2022 20:00 867801
S985 50000690 22.01.2022 12:00 22.01.2022 22:00 865394
S985 50000690 22.01.2022 12:00 22.01.2022 22:00 938541
randomly i chose 865394 and pasted it on 18.01 date.So on 18.01 date two people work instead of one.
Your help will be appreciated. Thank you.