简体   繁体   English

R tryCatch跳过错误

[英]R tryCatch skipping error

I tried concatenating 2 dataframes: Eset2 and Essential. 我尝试连接2个数据帧:Eset2和Essential。 They share 1 common column which contains gene names and both frames have unique rows. 它们共享一个包含基因名称的公共列,两个帧都有唯一的行。

So I decided to look up the values I need (RMA, ANNOT) in Eset2 and bind them to the row in Essential with corresponding gene name. 因此,我决定在Eset2中查找我需要的值(RMA,ANNOT),并将它们与相应的基因名称绑定到Essential中的行。

But sometimes I wouldn't be able to look up as there are unique genes in Essential. 但有时我不能抬头,因为Essential中有独特的基因。 So, there will be an error: my search for a corresponding row in Eset2 would turn up numeric(0). 因此,会出现错误:我在Eset2中搜索相应的行会变为数字(0)。

So I decide to use tryCatch. 所以我决定使用tryCatch。 However, it doesn't help. 但是,它没有帮助。 Instead of putting in NA for RMA and ANNOT when the search found no such gene in Eset2 the script puts in the values from the previous successfully found gene. 当搜索在Eset2中没有找到这样的基因时,脚本不会输入NA用于RMA和ANNOT,而是从先前成功找到的基因中输入值。

    for (i in 1:nrow(essential)) {
      temp <- tryCatch(
      # eset2$GENENAMEand essential[,4] contain gene names
        eset2[eset2$GENENAME == essential[i,4],]$RMA,
      error = function(e) {
        print("This is the 'error' part1")
        return(NA)}
      )
      essential$rma[i] <- temp

      temp <- tryCatch(
        eset2[eset2$GENENAME == essential[i,4],]$ANNOT,
      error = function(e) {
        print("This is the 'error' part2")
        return(NA)}
      )
      essential$long_name[i] <- temp
    }

I solved the issue by using this instead: 我用这个来解决这个问题:

        for (i in 1:nrow(essential)) {
            temp <- try(eset2[eset2$SYMBOL == essential[i,4],]$rma)
            if (length(temp) != 0) {
              essential$rma[i] <- temp
            }
            else {
              essential$rma[i] <- NA
            }
            temp <- try(eset2[eset2$SYMBOL == essential[i,4],]$GENENAME)
            if (length(temp) != 0) {
              essential$long_name[i] <- temp
            }
            else {
              essential$long_name[i] <- NA
            }
          }

I wonder if I'm using tryCatch wrong. 我想知道我是否使用了tryCatch错误。 I tried doing so: 我试过这样做:

         temp <- eset2[eset2$GENENAME == essential[i,4],]$ANNOT

But it didn't help. 但它没有帮助。 Can you see why my tryCatch failed? 你能明白为什么我的tryCatch失败了吗?

I believe the reason the error block is never being evaluated is that a data frame subset evaluating to numeric(0) is not an error. 我认为永远不会评估错误块的原因是评估为numeric(0)的数据帧子集不是错误。 Instead, you should just explicitly check for numeric(0) and replace with NA when you encounter it: 相反,您应该只是明确检查numeric(0)并在遇到它时替换为NA

for (i in 1:nrow(essential)) {
    temp <- eset2[eset2$GENENAME == essential[i,4],]$RMA
    essential$rma[i] <- ifelse(length(temp) == 0, NA, temp)

    temp <- eset2[eset2$GENENAME == essential[i,4],]$ANNOT
    essential$long_name[i] <- ifelse(length(temp) == 0, NA, temp)
}

By the way, there is probably a way to vectorize the for loop you used, possibly reducing the entire loop down to a few lines of R code. 顺便说一下,可能有一种方法可以for你使用的for循环进行矢量化,可能会将整个循环减少到几行R代码。 But this answer hopefully sheds some light on why the error block was never being hit, and what you can do as an alternative. 但是这个答案有希望揭示为什么错误块永远不会被击中,以及你可以做什么作为替代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM