基于字符串匹配创建新变量

时间:2017-03-24 20:25:32

标签: r if-statement

我有以下数据框:

df <- data.frame(Sample_name = c("01_00H_NA_DNA",   "01_00H_NA_RNA",    "01_00H_NA_S",  "01_00H_NW_DNA",    "01_00H_NW_RNA",    "01_00H_NW_S",  "01_00H_OM_DNA",    "01_00H_OM_RNA",    "01_00H_OM_S",  "01_00H_RL_DNA",    "01_00H_RL_RNA",    "01_00H_RL_S"),
             Pair = c("","", "S1","","","S2","","","S3","", "","S5"))

我正在尝试根据treatment创建一个新变量sample_name。我使用了以下代码:      df$treatment <- ifelse(grep("_NA_", df$sample_name, ignore.case = T), "nat", ifelse(grep("_NW_", df$sample_name, ignore.case = T), "natH2", ifelse(grep("_RL_", df$sample_name, ignore.case = T), "RNALat", ifelse(grep("_OM_", df$sample_name, ignore.case = T ), "Om"))))

我不明白我在这里做错了什么,我说错了 Error in $<-.data.frame(*tmp*, "treatment", value = logical(0)) : replacement has 0 rows, data has 12

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

得到了答案,在每个grep语句中添加了grepl:

df$treatment <- ifelse(grepl("_NA_", df$sample_name, ignore.case = T), "nat", 
                        ifelse(grepl("_NW_", df$sample_name, ignore.case = T ), "natH2",
                               ifelse(grepl("_RL_", df$sample_name, ignore.case = T), "RNALat",
                                      ifelse(grepl("_OM_", df$sample_name, ignore.case = T ), "Om", "NA"))))