我有以下数据框:
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
有什么建议吗?
答案 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"))))