В следующий раз, пожалуйста, подумайте о том, чтобы дать хотя бы одну запись о ситуациях, которые вы описываете. Я изменил ваш MWE, как показано ниже:
node_data <- structure(list(Ids = 1426:1431, Grade.and.Class = c("5B", "5B", "5B", "5B", "5B", "5B"),
color = c("darkgray", "darkgray", "darkgray",
"darkgray", "darkgray", "darkgray"),
onset = c(0, 0, 0, 0, 0, 0),
terminus = c(Inf, Inf, Inf, Inf, Inf, Inf), newid = 1:6,
Status.Day1 = c("Susceptible", "Susceptible", "Susceptible",
"Susceptible", 5, "Susceptible"),
Status.Day2 = c("Susceptible", "Susceptible", "Susceptible", "Susceptible",
"Susceptible","Susceptible")),
row.names = c(NA, 6L),
class = "data.frame")
edge_data <- structure(list(time_start = c(1, 1, 1, 1, 1, 1),
time_end = c(2, 2, 2, 2, 2, 2),
Person.1 = c(1558L, 1560L, 1567L, 1632L, 1632L, 1673L),
Person.2 = c(1567L, 1570L, 1574L, 1818L, 1866L, 1698L),
attrs = c("3B-3B", "3B-3B", "3B-3B", "4B-4B", "4B-4B", "1B-1B" ),
temp_id = c(1L, 1L, 1L, 1L, 1L, 1L),
temp_ing = c(1, 1, 1, 1, 1, 1),
from = c(59L, 60L, 64L, 86L, 86L, 103L),
to = c(64L, 65L, 67L, 191L, 215L, 116L),
Stats.day1 = c("Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible"),
newly.exposedday1 = c("No", "No", "No", 3, "No", 4)),
row.names = c(NA, 6L),
class = "data.frame")
Я думаю, это то, что вы ожидаете:
dt1names <- names(node_data)
dt <- merge(node_data,edge_data,by=NULL) %>%
mutate(Status.Day2 = ifelse((newid %in% Status.Day1 | newid %in% newly.exposedday1), "Inf", Status.Day2)) %>%
dplyr::select(all_of(dt1names)) %>% distinct()
> dt
Ids Grade.and.Class color onset terminus newid Status.Day1 Status.Day2
1 1426 5B darkgray 0 Inf 1 Susceptible Susceptible
2 1427 5B darkgray 0 Inf 2 Susceptible Susceptible
3 1428 5B darkgray 0 Inf 3 Susceptible Inf
4 1429 5B darkgray 0 Inf 4 Susceptible Inf
5 1430 5B darkgray 0 Inf 5 5 Inf
6 1431 5B darkgray 0 Inf 6 Susceptible Susceptible