Вы можете использовать case_when
и lead
из пакета dplyr
:
library(dplyr)
data %>%
mutate(Name = case_when(Name == "-" ~ lead(Name),
TRUE ~ Name))
Role Name Time Date Value
1 Field Support John 6:00 AM - 6:59 AM 1/1/2020 9
2 Field Support John 7:00 AM - 7:59 AM 1/1/2020 4
3 Field Support John 8:00 AM - 8:59 AM 1/1/2020 4
Data
data <- structure(list(Role = structure(c(1L, 1L, 1L), .Label = "Field Support", class = "factor"),
Name = structure(c(1L, 2L, 2L), .Label = c("-", "John"), class = "factor"),
Time = structure(1:3, .Label = c("6:00 AM - 6:59 AM", "7:00 AM - 7:59 AM",
"8:00 AM - 8:59 AM"), class = "factor"), Date = structure(c(1L,
1L, 1L), .Label = "1/1/2020", class = "factor"), Value = c(9L,
4L, 4L)), class = "data.frame", row.names = c(NA, -3L))