附完整数据:
id = c (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
vm = c("12", "13", "14", "15", "16", "17", "18" "19", "20", "21", "22", "23", "24")
GE = c("0", "0", "0", "0", "0" "0", "1", "0", "1", "0", "1","0", "1")
status = c("noinfection", "noinfection", "noinfection", "noinfection", "noinfection", "noinfection", "infection", "noinfection", "infection", "noinfection", "infection", "noinfection", "infection")
fichier <- data.frame(id, vm, GE, status)
我们已经获得了 18 个月和 24 个月访视时的 GE 变量值。无法将它们从我们的数据集中删除。我如何更改这些值,以便它们考虑到以前的访问?
GE = 0,如果所有之前的访问都值 0。
GE = 1,如果之前至少有一次访问值 1。
根据您的上一个问题,这些解决方案将会起作用。
场景 1a:使用“完整”数据更新场景 1
这可以使您的原始数据保持不变,例如即使 vm == 18 在“完整”数据中具有 GE == 1,状态仍将为“无感染”。我创建了 GE1 列,以便您可以看到该过程。如果您不希望最终结果中出现 GE1 列,请将 GE1 的所有实例更改为 tmp1:
library(dplyr)
library(tidyr)
id <- c (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
vm <- c("12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24")
GE <- c("0", "0", "0", "0", "0", "0", "1", "0", "1", "0", "1","0", "1")
status <- c("noinfection", "noinfection", "noinfection", "noinfection", "noinfection", "noinfection", "infection", "noinfection", "infection", "noinfection", "infection", "noinfection", "infection")
fichier <- data.frame(id, vm, GE, status)
fichier <- fichier |>
mutate(across(c(vm, GE), as.integer)) |>
group_by(id) |> # assuming your data may have multiple id values
mutate(tmp = if_else(vm <= 18, 1, 2)) |>
group_by(id, tmp) |>
mutate(GE1 = if_else(vm %in% c(18, 24), NA, GE),
GE1 = case_when(is.na(GE1) & sum(GE1, na.rm = TRUE) == 0 ~ 0,
is.na(GE1) & sum(GE1, na.rm = TRUE) > 0 ~ 1,
.default = GE1),
status = if_else(GE1 == 1, "infection", "noinfection")) |>
ungroup() |>
select(-starts_with("tmp"))
场景 2a:基于“完整”数据更新场景 1
id <- c (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
vm <- c("12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24")
GE <- c("0", "0", "0", "0", "0", "0", "1", "0", "1", "0", "1","0", "1")
status <- c("noinfection", "noinfection", "noinfection", "noinfection", "noinfection", "noinfection", "infection", "noinfection", "infection", "noinfection", "infection", "noinfection", "infection")
fichier <- data.frame(id, vm, GE, status)
fichier <- fichier |>
mutate(across(c(vm, GE), as.integer)) |>
group_by(id) |> # assuming your data may have multiple id values
mutate(tmp = if_else(vm <= 18, 1, 2)) |>
group_by(id, tmp) |>
mutate(GE1 = if_else(vm %in% c(18, 24), NA, GE),
tmp1 = +(n_distinct(GE1, na.rm = TRUE) == 1),
GE1 = case_when(is.na(GE1) & tmp1 == 1 & first(GE1) == 0 ~ 0,
is.na(GE1) & tmp1 == 1 & first(GE1) == 1 ~ 1,
.default = GE1),
status = case_when(GE1 == 0 ~ "noinfection",
GE1 == 1 ~ "infection",
.default = "mixed")) |>
ungroup() |>
select(-starts_with("tmp"))