Используя dplyr
, сначала убедитесь, что столбцы с датами имеют формат Date
. Затем измените столбец All_Star
, если Date_Career_High
следует после Date_All_Star
и All_Star == "Y"
.
library(dplyr)
df %>%
mutate_at(vars(starts_with("Date")), as.Date, format = "%m/%d/%y") %>%
mutate(All_Star = ifelse((Date_Career_High > Date_All_Star) & (All_Star == "Y"), "P", All_Star))
Выход
Player Date_Career_High Date_All_Star All_Star
1 A 2020-02-07 2020-11-12 Y
2 B 2020-05-04 2020-12-12 Y
3 C 2020-06-02 2019-10-12 N
4 D 2020-11-19 2020-11-12 P
5 E 2020-12-14 2020-11-12 P
6 F 2020-03-15 2019-10-12 N
Данные
df <- structure(list(Player = c("A", "B", "C", "D", "E", "F"), Date_Career_High = c("02/07/20",
"05/04/20", "06/02/20", "11/19/20", "12/14/20", "03/15/20"),
Date_All_Star = c("11/12/20", "12/12/20", "10/12/19", "11/12/20",
"11/12/20", "10/12/19"), All_Star = c("Y", "Y", "N", "Y",
"Y", "N")), class = "data.frame", row.names = c(NA, -6L))