jeudi 24 août 2017

For loop using if and else function due to 'Not enough finite observation' error in R

I am trying to use the spearman correlation test for different variables per year. I have this:

    year <- c(1986:2013)
    spearman_coef_IBD <- c(NA)
    spearman_coef_inbrecoef <- c(NA)
    spearman_coef_inbrecoef_mate <- c(NA)
    spearman_coef_het <- c(NA)
    spearman_coef_het_mate <- c(NA)
    spearman_fem <- data.frame(year, spearman_coef_IBD,                               spearman_coef_inbrecoef, spearman_coef_inbrecoef_mate, spearman_coef_het, spearman_coef_het_mate)
    for(j in c(1990:2013)) {
       dispdata_w_mates <- subset(dispdata_w_mates_fem, YrFirstBred == j)
       dispdata_w_mates$Pairs <- with(dispdata_w_mates, paste(ID, Mate_ID, sep=" "))
       typeA <- genome$IBD[match(dispdata_w_mates$Pairs, genome$pair1)]
       typeB <- genome$IBD[match(dispdata_w_mates$Pairs, genome$pair2)]
       dispdata_w_mates$relatedness <- ifelse(is.na(typeA), typeB, typeA)
       dispdata_w_mates_w_HET <- merge(dispdata_w_mates, het, by.x='ID', by.y='IID', all.x = TRUE)[, c(1:27, 33)]
       colnames(dispdata_w_mates_w_HET)[28] <- 'HET_ID'
       dispdata_w_mates_w_HET2 <- merge(dispdata_w_mates, het, by.x= 'Mate_ID', by.y='IID', all.x = TRUE)[, c(1:27, 33)]
       colnames(dispdata_w_mates_w_HET2)[28] <- 'HET_MATE'
       dispdata_w_mates_w_inbcoef <- merge(dispdata_w_mates, inb_coef, by.x='ID', by.y='IID')[, c(1:27, 32)]
       colnames(dispdata_w_mates_w_inbcoef)[28] <- 'inbcoef_ID'
       dispdata_w_mates_w_inbcoef2 <- merge(dispdata_w_mates, inb_coef, by.x= 'Mate_ID', by.y='IID')[, c(1:27, 32)]
       colnames(dispdata_w_mates_w_inbcoef2)[28] <- 'inbcoef_MATE'

       spearman_fem[which(spearman_fem$year==j), "spearman_coef_IBD"] <- cor.test(dispdata_w_mates$NatalDispDom, dispdata_w_mates$relatedness, method = "spearman")
       spearman_fem[which(spearman_fem$year==j), "spearman_coef_inbrecoef"] <- cor.test(dispdata_w_mates_w_inbcoef$NatalDispDom, dispdata_w_mates_w_inbcoef$inbcoef_ID, method = "spearman")
       spearman_fem[which(spearman_fem$year==j), "spearman_coef_inbrecoef_mate"] <- cor.test(dispdata_w_mates_w_inbcoef2$NatalDispDom, dispdata_w_mates_w_inbcoef2$inbcoef_MATE, method = "spearman")
       spearman_fem[which(spearman_fem$year==j), "spearman_coef_het"] <- cor.test(dispdata_w_mates_w_HET$NatalDispDom, dispdata_w_mates_w_HET$HET_ID, method = "spearman")
       spearman_fem[which(spearman_fem$year==j), "spearman_coef_het_mate"] <- cor.test(dispdata_w_mates_w_HET2$NatalDispDom, dispdata_w_mates_w_HET2$HET_MATE, method = "spearman")
    }

but it keeps returning this: "Error in cor.test.default(dispdata_w_mates$NatalDispDom, dispdata_w_mates$relatedness, : not enough finite observations"

I know it is because in some years there are not enough values for the spearman test to be conducted, such as in year 1986 there are no values for dispdata_w_mates$relatedness or in 1990, there is only one value for dispdata_w_mates_w_inbcoef$inbcoef_ID. How do I use the if else function to put in NA in the columns where there are no values so that it can continue doing the loop for the other years?

Aucun commentaire:

Enregistrer un commentaire