[R] z[j,]$a v. z$a[j]

Bartz, Kevin Kevin.Bartz at FMR.COM
Tue Jul 16 19:31:38 CEST 2002


I noticed that R takes much longer to pull an element from a data frame when
I select the row first (z[j,]$a) than when I select the column first
(z$a[j]).

> data.frame(a = 1:10, b = letters[1:10], c = LETTERS[1:10]) -> z
> z
    a b c
1   1 a A
2   2 b B
3   3 c C
4   4 d D
5   5 e E
6   6 f F
7   7 g G
8   8 h H
9   9 i I
10 10 j J
> system.time(for (i in 1:1000) for (j in 1:10) z$a[j])
[1] 0.96 0.02 0.99 0.00 0.00
> system.time(for (i in 1:1000) for (j in 1:10) z[j,]$a)
[1] 46.20  6.04 52.55  0.00  0.00

Why is this?

> R.version
         _                   
platform sparc-sun-solaris2.6
arch     sparc               
os       solaris2.6          
system   sparc, solaris2.6   
status                       
major    1                   
minor    5.1                 
year     2002                
month    06                  
day      17                  
language R                 

Thanks,

Kevin
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list