Note that there are some explanatory texts on larger screens.

plurals
  1. POLooping Survdiff
    text
    copied!<p>Fairly simple question, but my R knowledge is not great. </p> <p><strong>Introduction</strong></p> <p>I'm using the package Survdiff. Using it once is fine, but I want to loop it through many variables. </p> <p>For example: I have a data.frame of 43000 variables with 177 observations. The data.frame also has 3 initial variables; first one is just an identity number, second one is 'time' and third is 'cens'. Both time and cens are needed for the Survdiff package. But from this, we know that we want to loop through the remaining variables in the data.frame i.e. Genedata[,4:43000]</p> <p>What I've done so Far I would normally use the for statement but in R i'm aware that this is potentially much slower than the apply statement. </p> <pre><code>cols &lt;- as.list( names(Genedata) ) #generate a list of column numbers Geneset &lt;- lapply(cols , function (x) { survdiff( Surv( Genedata$time, Genedata$cens=="1" ) ~ x , data = Genedata )$chisq } ) </code></pre> <p>However - I am getting an error </p> <pre><code>"Error in model.frame.default(formula = Surv(Genedata$time, Genedata$cens == : variable lengths differ (found for 'x')" </code></pre> <p><strong>Sample of the data</strong> (First 15 rows and 8 columns) - <strong>note: the headers have overflowed</strong></p> <pre><code>Accession time cens A1BG.229819_at A1BG_AS1.232462_s_at A1CF.220951_s_at A1CF.241547_at A2LD1.232422_at 1 120 0 0 0 0 0 1 2 120 0 0 0 0 0 1 3 28.96 NA 1 0 0 0 1 4 119.21 0 0 0 0 0 1 5 59.53 0 0 1 0 0 1 6 68.81 1 0 0 0 0 1 7 82.29 0 0 0 0 0 1 8 110.82 0 0 0 0 0 1 9 65.88 NA 1 0 0 0 1 10 84.13 0 0 0 0 0 1 11 16.47 NA 0 0 0 0 1 12 89.75 0 0 0 1 1 1 13 76.07 0 0 0 0 0 1 14 67.82 0 0 0 0 0 1 </code></pre> <p><strong>Problems</strong></p> <ol> <li>I'm not sure how to properly use 'apply' in this situation, because the function is more complex.</li> </ol> <p><strong>Update1</strong></p> <p>Changed to FUN=function(x).... now error is gone. Instead I get</p> <pre><code>dim(X) must have a positive length </code></pre> <p>yet dim(GeneMatrix) returns positive values...</p> <p><strong>Update2</strong></p> <p>Updated code after more tweaks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload