Reputation: 2385
I have a function tryCatch that outputs something like this:
> s1 <- tryCatch(survdiff(Surv(as.numeric(as.character(ClinicalDataHep$new_death))[ind_clin],ClinicalDataHep$death_event[ind_clin])~event_rna[ind_gene,ind_tum]), error = function(e) return(NA))
> s1
Call:
survdiff(formula = Surv(as.numeric(as.character(ClinicalDataHep$new_death))[ind_clin],
ClinicalDataHep$death_event[ind_clin]) ~ event_rna[ind_gene,
ind_tum])
N Observed Expected (O-E)^2/E (O-E)^2/V
event_rna[ind_gene, ind_tum]=0 72 41 41.1 0.000223 0.00278
event_rna[ind_gene, ind_tum]=1 4 4 3.9 0.002345 0.00278
Chisq= 0 on 1 degrees of freedom, p= 0.958
From this output it is possible to calculate a p-value like this:
> p.val <- 1 - pchisq(s1$chisq, length(s1$n) - 1)
> p.val
[1] 0.9579535
I would like to make a for-loop in the first function that takes all possible ind_gene
in TryCatch
which is defined from rownames(matrix_cpm_spike_liver)
and get p-values for all the different rownames.
Examples of ind_gene
:
> head(rownames(matrix_cpm_spike_liver))
[1] "hsa-let-7a-2-3p" "hsa-let-7a-3p" "hsa-let-7a-5p" "hsa-let-7b-3p" "hsa-let-7b-5p"
[6] "hsa-let-7c-3p"
Something like:
for (i in rownames(matrix_cpm_spike_liver))
{TryCatch...
}
Upvotes: 2
Views: 68
Reputation: 1438
One way of doing it is wrapping a function around everything and use sapply
. This way you do not have to change your code much. To make the loop successful, we must handle the cases where the tryCatch
catches an error. This is done with an ifelse
on the p-value.
mycatch <- function(ind_gene){
s1 <- tryCatch(survdiff(Surv(as.numeric(as.character(
ClinicalDataHep$new_death)
)[ind_clin], ClinicalDataHep$death_event[ind_clin]) ~
event_rna[ind_gene,ind_tum]),
error = function(e) return(NA))
p.val <- ifelse(is.na(s1), # Condition
NA, # Return if s1 = NA
1 - pchisq(s1$chisq, length(s1$n) - 1))
p.val
}
sapply(rownames(matrix_cpm_spike_liver), mycatch)
Notice how I have split your lines of code - it is generally considered good practice to not have too long lines of code. I use a max of 65 since I occasionally paste it into TeX documents but as you can see by the grey box above, they can be much wider.
Upvotes: 2