R语言:基于多列二进制数据添加新列的实现问题
问题:在R数据框中新增病毒阳性标记列失败,求可行方案
无法在R的数据框中添加新列,需求是新增一列,标记所有病毒检测列中是否存在数值"1"(阳性),该列将用于后续概率计算。
样本数据
Filovirus (MOD) PCR : Phlebo (Sanchez-Seco) PCR 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 species code forest site <fctr> <dbl> <fctr> SM 1 UMNP-mangabey SM 1 UMNP-mangabey RC 9 UMNP-hondohondoc BWC 9 UMNP-hondohondod BWC 9 UMNP-hondohondod BWC 9 UMNP-hondohondod BWC 9 UMNP-hondohondod BWC 9 UMNP-hondohondod BWC 9 UMNP-hondohondod BWC 9 UMNP-hondohondod
尝试过的代码(未成功)
tmp=which(data==1,arr.ind=T) tmp=tmp[order(tmp[,"row"]),] c("positive","negative")[tmp[,"col"]] -> data$new
数据的dput结构
structure(list(`Filovirus (MOD) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Filovirus (A) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Filovirus (B) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Filo C PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Filovirus (D) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Coronavirus (Quan) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Coronavirus (Watanabe) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Paramyxo (Tong) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Flavivirus Moureau PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Flavivirus Sanchez-seco PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Arena Lozano 1 PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Retrovirus Courgnard PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Simian Foamy Goldberg (Pol) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Simian Foamy Goldberg (LTR Region) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Influenza (Anthony) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Influenza (Liang) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Rhabdo (CII) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Enterovirus CII I PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Enterovirus CII-II PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Alphav (Sanchez-Seco) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Lyssavirus (Vasquez-Moron) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Seadornavirus (CII) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Hantavirus (Raboni) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Hantavirus (Klempa) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Nipah (Wacharapleusadee) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Henipa (Feldman) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Bunya S (Briese) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Bunya L (Briese) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), `Phlebo (Sanchez-Seco) PCR` = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), species = structure(c(3L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("SM", "SY", "BWC", "YB", "RC"), class = "factor"), code = c(2, 5, 5, 5, 5, 5, 5, 5, 5, 5), forestsite = structure(c(3L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L), .Label = c("Magombera1", "Magombera2", "NDUFR", "Ndundulu1", "Ndundulu2", "Ndundulu3", "Nyumbanitu", "UMNP-campsite3", "UMNP-hondohondoa", "UMNP-hondohondob", "UMNP-hondohondoc", "UMNP-hondohondod", "UMNP-hondohondoe", "UMNP-HQ", "MamaGoti", "UMNP-mangabey", "UMNP-njokamoni", "UMNP-Sanje1", "UMNP-Sanje2", "UMNP-Sanje3", "Sonjo", "SonjoRoad" ), class = "factor")), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))
可行解决方案
问题根源
原代码失败的原因有两个:
- 病毒检测列存储为字符型("0"),而非数值型,
data==1无法匹配到任何值; - 逻辑错误:原代码试图按列索引生成标记,但实际需要行级判断——判断每行是否存在至少一个"1"。
方法1:Base R实现(无需额外包)
# 1. 提取所有病毒检测列(列名包含PCR) pcr_cols <- grep("PCR", names(data), value = TRUE) # 2. 可选:将字符型的检测列转为数值型,方便后续计算 data[pcr_cols] <- lapply(data[pcr_cols], as.numeric) # 3. 逐行判断是否存在阳性(1),生成标记列 data$positive_status <- ifelse( rowSums(data[pcr_cols] == 1, na.rm = TRUE) > 0, "positive", "negative" )
方法2:tidyverse/dplyr实现(更简洁)
如果习惯使用tidyverse语法:
library(dplyr) data <- data %>% # 转换PCR列为数值型(可选) mutate(across(contains("PCR"), as.numeric)) %>% # 生成阳性标记列:任意PCR列等于1则标记为positive mutate(positive_status = case_when( if_any(contains("PCR"), ~ .x == 1) ~ "positive", TRUE ~ "negative" ))
验证结果
运行代码后,positive_status列会标记每行是否存在病毒阳性结果,可直接用于后续概率计算。
内容的提问来源于stack exchange,提问作者Marnee Roundtree
相关产品推荐
相关产品推荐

