if / else grepl“参数长度为零”

时间:2019-08-08 19:59:02

标签: r

我想对列表中的多个数据帧执行一组操作(在R中)。具体来说,我为每个数据库创建一个“库”列,然后使用该列来确定要执行哪种过滤操作。这是实际的代码:

sampleList <- list(RNA1 = "data/not_processed/dedup.Bp1R4T2_S2.txt",
              RNA2 = "data/not_processed/dedup.Bp1R4T3_S4.txt",
              RNA3 = "data/not_processed/dedup.Bp1R5T2_S1.txt",
              RNA4 = "data/not_processed/dedup.Bp1R5T3_S2.txt",
              RNA5 = "data/not_processed/dedup.Bp1R14T5_S1.txt",
              RNA6 = "data/not_processed/dedup.Bp1R14T6_S1.txt",
              RNA7 = "data/not_processed/dedup.Bp1R14T6_S2.txt",
              RNA8 = "data/not_processed/dedup.Bp1R14T7_S2.txt",
              RNA9 = "data/not_processed/dedup.Bp1R14T8_S3.txt",
              RNA10 = "data/not_processed/dedup.Bp1R14T9_S3.txt",
              RNA11 = "data/not_processed/dedup.Bp1R14T9_S4.txt",
              DNA1 = "data/not_processed/dedup.dna10_1_S4.txt",
              DNA2 = "data/not_processed/dedup.dna10_2_S5.txt",
              DNA3 = "data/not_processed/dedup.dna10_3_S6.txt",
              DNA4 = "data/not_processed/dedup.dna50_1_S1.txt",
              DNA5 = "data/not_processed/dedup.dna50_2_S2.txt",
              DNA6 = "data/not_processed/dedup.dna50_3_S3.txt",
              DNA7 = "data/not_processed/dedup.dna50_pcrcocktail_S7.txt")

batch <- lapply(names(sampleList),function(mysample){
  aux <- read.table(sampleList[[mysample]], col.names=c(column1, column2, ..., ID, library, column4, etc...))
  aux %>% mutate(library = mysample, R = Fw_ref + Rv_ref, A = Fw_alt + Rv_alt) %>% distinct(ID, .keep_all=T)
  if (grepl("DNA", aux$library)){
    aux %>% filter(aux$R>1 & aux$A>1)
  } else {
    aux %>% filter((aux$R+aux$A)>7 & aux$Fw_ref>=1 & aux$Rv_ref>=1 & aux$Fw_alt>=1 & aux$Rv_alt>=1)
  }
  aux
})

batch_file <- do.call(rbind, batch)
write.table(batch_file, "data/batch_file.txt", col.names = T, sep = "\t")

库列的可能值为DNA1到DNA7,以及RNA1到11。我也尝试使用“ char”%in%,但是它也出现了相同的问题:

if(grepl(“ DNA”,aux $ library)){错误:参数长度为零

好像if条件不能识别库中的值。但是,当我尝试将if / else条件应用于batch_file(未过滤,基本上是使用此代码获得的,不含if / else部分)时,效果很好。

非常感谢。

0 个答案:

没有答案