[R] suprising behaviour of tryCatch()

Calboli Federico (LUKE) |eder|co@c@|bo|| @end|ng |rom |uke@||
Wed May 17 12:42:17 CEST 2023


Hello,

I run a fisher.test() in a loop, with the issue that some of the data will not be useable. To protect the loop I used tryCatch but:

sexsnp = rep(NA, 1750)
for(i in 1:1750){tryCatch(sexsnp[i] = fisher.test(table(data[,3], data[,i + 38]))$p, error = function(e) print(NA))}
Error: unexpected '=' in "for(i in 1:1750){tryCatch(sexsnp[i] ="

But this works:

for(i in 1:1750){tryCatch(sexsnp[i] <- fisher.test(table(data[,3], data[,i + 38]))$p, error = function(e) print(NA))}
[1] NA
[1] NA
[1] NA

The only difference is the use of �=� or �<-� to assign the p value of the fisher test to the vector.  I stopped using �<-� eons ago so it took a bit to figure out.

Tested on R 4.1.2 on ContOS 8 , and on  R 4.3.0 on a M1 mac with the same result.  I�d be obliged if someone can explain why tryCatch assigns items with �<-� and not �=�.

Cheers

F

Federico Calboli
Tutkija
Genomiikka ja jalostus
Luonnonvarakeskus

	[[alternative HTML version deleted]]



More information about the R-help mailing list