[R] z[j,]$a v. z$a[j]
ripley@stats.ox.ac.uk
ripley at stats.ox.ac.uk
Tue Jul 16 19:46:49 CEST 2002
On Tue, 16 Jul 2002, Bartz, Kevin wrote:
> 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?
Because a row of a data frame is still a data frame, a much more complex
object than a column of a data frame.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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