r - For-loop of rownames input to function -


i have function trycatch outputs 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 output possible calculate p-value this:

> p.val <- 1 - pchisq(s1$chisq, length(s1$n) - 1) > p.val [1] 0.9579535 

i make for-loop in first function takes possible ind_gene in trycatch defined rownames(matrix_cpm_spike_liver) , get p-values 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... } 

one way of doing wrapping function around , use sapply. way not have change code much. make loop successful, must handle cases trycatch catches error. done ifelse on 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 have split lines of code - considered practice not have long lines of code. use max of 65 since paste tex documents can see grey box above, can wider.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -