基于历史GE值补全vm=18/24的status变量的R技术需求
现有数据
id = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2) vm = c("13", "14", "15", "16", "17", "19", "20", "21", "22", "23") GE = c("0", "0", "0", "0", "0", "0", "1", "0", "1", "0") fichier <- data.frame(id, vm, GE)
已实现的R代码
library(tidyverse) fichier <- fichier %>% mutate(statut = case_when( fichier$vm == 13 & fichier$GE == 1 ~ "infection", fichier$vm == 14 & fichier$GE == 1 ~ "infection", fichier$vm == 15 & fichier$GE == 1 ~ "infection", fichier$vm == 16 & fichier$GE == 1 ~ "infection", fichier$vm == 17 & fichier$GE == 1 ~ "infection", fichier$vm == 19 & fichier$GE == 1 ~ "infection", fichier$vm == 20 & fichier$GE == 1 ~ "infection", fichier$vm == 21 & fichier$GE == 1 ~ "infection", fichier$vm == 22 & fichier$GE == 1 ~ "infection", fichier$vm == 23 & fichier$GE == 1 ~ "infection", TRUE ~ "noinfection") )
当前运行结果
id = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2), vm = c("13", "14", "15", "16", "17", "19", "20", "21", "22", "23"), GE = c("0", "0", "0", "0", "0", "0", "1", "0", "1", "0"), statut = c("noinfection", "noinfection", "noinfection", "noinfection", "noinfection", "noinfection", "infection", "noinfection", "infecton", "noinfection") fichier <- data.frame(id, vm, GE, status)
需求
- a) 为
vm=18的status变量赋值:若13至17访视的GE值均为0,则赋值0;若13至17访视中存在GE=1,则赋值1。 - b) 为
vm=24的status变量赋值:若19至23访视的GE值均为0,则赋值0;若19至23访视中存在GE=1,则赋值1。
内容的提问来源于stack exchange,提问作者Pipas
相关产品推荐
相关产品推荐

