R tryCatch跳过错误

时间:2016-09-28 15:00:36

标签: r dataframe error-handling

我尝试连接2个数据帧:Eset2和Essential。它们共享一个包含基因名称的公共列,两个帧都有唯一的行。

所以我决定在Eset2中查找我需要的值(RMA,ANNOT)并将它们绑定到Essential中具有相应基因名称的行。

但有时我无法抬头,因为Essential中有独特的基因。因此,会出现错误:我在Eset2中搜索相应的行会变为数字(0)。

所以我决定使用tryCatch。 但是,它没有帮助。当搜索在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
    }

我用这个来解决这个问题:

        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
            }
          }

我想知道我是否使用了tryCatch错误。 我试过这样做:

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

但它没有帮助。 你能明白为什么我的tryCatch失败了吗?

1 个答案:

答案 0 :(得分:0)

我认为永远不会评估错误块的原因是评估为numeric(0)的数据帧子集不是错误。相反,您应该明确检查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) }

for

顺便说一下,可能有一种方法可以向你使用的D:\Eon_Final_Build\EON_android\app\src\main\AndroidManifest.xml:96:13-72 Error: Attribute activity#com.facebook.FacebookActivity@theme value=(@android:style/Theme.Translucent.NoTitleBar) from AndroidManifest.xml:96:13-72 is also present at [com.facebook.android:facebook-android-sdk:4.16.0] AndroidManifest.xml:32:13-63 value=(@style/com_facebook_activity_theme). Suggestion: add 'tools:replace="android:theme"' to <activity> element at AndroidManifest.xml:92:9-96:75 to override. See http://g.co/androidstudio/manifest-merger for more information about the manifest merger. :app:processDebugManifest FAILED Error:Execution failed for task ':app:processDebugManifest'. Manifest merger failed : Attribute activity#com.facebook.FacebookActivity@theme value=(@android:style/Theme.Translucent.NoTitleBar) from AndroidManifest.xml:96:13-72 is also present at [com.facebook.android:facebook-android-sdk:4.16.0] AndroidManifest.xml:32:13-63 value=(@style/com_facebook_activity_theme). Suggestion: add 'tools:replace="android:theme"' to <activity> element at AndroidManifest.xml:92:9-96:75 to override. Information:BUILD FAILED 循环进行矢量化,可能会将整个循环减少到几行R代码。但是这个答案有希望揭示为什么错误块永远不会被击中,以及你可以做什么作为替代。

相关问题