R:循环中的Pearson相关,防止在发生错误时停止并输出NA

时间:2017-08-03 14:55:06

标签: r

我希望运行矩阵(dat)的每一行与矢量(v1)的Pearson相关性,作为循环的一部分,并在表格中输出相关系数和相关的p值。以下是随机数据的示例(最后粘贴的数据):

result_table <- data.frame(matrix(ncol = 2, nrow = nrow(dat)))
colnames(result_table) <- c("correlation_coefficient", "pvalue")

for(i in 1:nrow(dat)){
  print(i)
  corr <- cor.test(as.numeric(dat[i,]), v1, na.action = "na.omit")
  result_table[i,1] <- corr$estimate
  result_table[i,2] <- corr$p.value
}

当cor.test()删除缺失的数据时,有时剩余的观察结果不足,循环因错误而停止(例如第11行)。我希望循环继续运行,只需将结果表中的值保留为NA。我认为结果表应如下所示:

> result_table
   correlation_coefficient     pvalue
1               0.68422642 0.04206591
2              -0.15895586 0.70694013
3              -0.37005028 0.53982309
4               0.08448970 0.89255250
5               0.86860091 0.05603661
6               0.19544883 0.75274040
7              -0.94695380 0.01454887
8              -0.03817885 0.94275955
9              -0.15214122 0.77354897
10             -0.22997890 0.70978386
11                      NA         NA
12                      NA         NA
13             -0.27769887 0.59415930
14             -0.09768153 0.81800885
15             -0.20986632 0.61790214
16             -0.40474976 0.31990456
17             -0.00605937 0.98863896
18              0.02176976 0.95919460
19             -0.14755097 0.72733118
20             -0.25830856 0.50216600

我还希望错误继续打印

以下是数据:

> dput(v1)
c(-0.840396, 0.4746047, -1.101857, 0.5164767, 1.2203134, -0.9758888, 
-0.3657913, -0.6272523, -0.5853803, 1.7367901)

> dput(dat)
structure(list(s1 = c(-0.52411895, 0.14709633, 0.05433954, 0.7504406, 
-0.59971988, -0.59679685, -0.12571854, 0.73289705, -0.71668771, 
-0.04813957, -0.67849896, -0.11947141, -0.26371884, -1.34137162, 
2.60928064, -1.23397547, 0.51811222, -4.10759883, -0.70127093, 
7.51914575), s2 = c(0.21446623, -0.27281487, NA, NA, NA, NA, 
NA, NA, -0.62468391, NA, NA, NA, -3.84387999, 0.64010069, NA, 
NA, NA, NA, NA, NA), s3 = c(0.3461212, 0.279062, NA, NA, NA, 
-0.4737744, 0.6313365, -2.8472641, 1.2647846, 2.2524449, -0.7913039, 
-0.752590307, -3.535815266, 1.692385187, 3.55789764, -1.694910854, 
-3.624517121, -4.963855198, 2.395998161, 5.35680032), s4 = c(0.3579742, 
0.3522745, -1.1720907, 0.4223402, 0.146605, -0.3175295, -1.383926807, 
-0.688551166, NA, NA, NA, NA, NA, 0.703612974, 1.79890268, -2.625404608, 
-3.235884921, -2.845474098, 0.058650461, 1.83900702), s5 = c(1.698104376, 
NA, NA, NA, NA, NA, -1.488000007, -0.739488766, 0.276012387, 
0.49344994, NA, NA, -1.417434166, -0.644962513, 0.04010434, -3.388182254, 
2.900252493, -1.493417096, -2.852256003, -0.98871696), s6 = c(0.3419271, 
0.2482013, -1.2230283, 0.270752, -0.6653978, -1.1357202, NA, 
NA, NA, NA, NA, NA, NA, NA, -1.0288213, -1.17817328, 6.1682455, 
1.02759131, -3.80372867, -2.6249692), s7 = c(0.3957243, 0.8758406, 
NA, NA, NA, NA, NA, 0.60196247, -1.28631859, -0.5754757, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, -2.6303001), s8 = c(-0.26409595, 
1.2643281, 0.05687957, -0.09459169, -0.7875279, NA, NA, NA, NA, 
NA, NA, NA, 2.42442997, -0.00445559, -1.0341522, 2.47315322, 
0.1190265, 5.82533417, 0.82239131, -0.8279679), s9 = c(0.237123, 
-0.5004619, 0.4447322, -0.2155249, -0.2331443, 1.3438071, -0.3817672, 
1.9228182, 0.305661, -0.01348, NA, NA, 3.4009042, 0.8268469, 
0.2061843, -1.1228663, -0.1443778, 4.8789902, 1.3480328, 0.4258486
), s10 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
0.5211859, 0.2196643, -1.2333367, 0.1186947, 1.478086, 0.5211859, 
0.2196643)), .Names = c("s1", "s2", "s3", "s4", "s5", "s6", "s7", 
"s8", "s9", "s10"), class = "data.frame", row.names = c(NA, -20L
))

2 个答案:

答案 0 :(得分:1)

tryCatch的解决方案可能是

for(i in 1:nrow(dat)){
  print(i)
  corr <- tryCatch(cor.test(as.numeric(dat[i,]), v1, na.action = "na.omit"), error = function(e) return(NA))
  if(length(corr) == 1){
      result_table[i,1] <- NA
      result_table[i,2] <- NA
  }else{
      result_table[i,1] <- corr$estimate
      result_table[i,2] <- corr$p.value
  }
}

答案 1 :(得分:0)

以下是tryCatch()的解决方案:

用以下代码替换for循环:

for(i in 1:nrow(dat)){
  tryCatch({
    print(i)
    corr <- cor.test(as.numeric(dat[i,]), v1, na.action = "na.omit") # Correlation miRNA activity vs CNVs for that gene
    result_table[i,1] <- corr$estimate
    result_table[i,2] <- corr$p.value
  }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}