[R] Which is more efficient?

Matt Curcio matt.curcio.ri at gmail.com
Fri Aug 5 05:19:10 CEST 2011


Greetings all,
I am curious to know if either of these two sets of code is more efficient?

Example1:
 ## t-test ##
colA <- temp [ , j ]
colB <- temp [ , k ]
ttr <- t.test ( colA, colB, var.equal=TRUE)
tt_pvalue [ i ] <- ttr$p.value

or
Example2:
tt_pvalue [ i ] <- t.test ( temp[ , j ], temp[ , k ], var.equal=TRUE)
-------------
I have three loops, i, j, k.
One to test the all of <i> files in a directory.  One to tease out
column <j> and compare it by means of t-test to column <k> in each of
the files.
---------------
for ( i in 1:num_files ) {
   temp <- read.table ( files_to_test [ i ], header=TRUE, sep="\t")
   num_cols <- ncol ( temp )
   ## Define Columns To Compare ##
   for ( j in 2 : num_cols ) {
      for ( k in 3 : num_cols ) {
          ## t-test ##
          colA <- temp [ , j ]
          colB <- temp [ , k ]
          ttr <- t.test ( colA, colB, var.equal=TRUE)
          tt_pvalue [ i ] <- ttr$p.value
      }
   }
}
--------------------------------
I am a novice writer of code and am interested to hear if there are
any (dis)advantages to one way or the other.
M


Matt Curcio
M: 401-316-5358
E: matt.curcio.ri at gmail.com



More information about the R-help mailing list