[R] Speed Optimization: Faster Way of Accessing data.frame in a Loop?

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Nov 11 06:22:30 CET 2006


I don't see how this code can work at all. '[<-.data.frame' does not 
accept three arguments, e.g.

> women[1,2,3] <- 17
Error in `[<-.data.frame`(`*tmp*`, 1, 2, 3, value = 17) :
         unused argument(s) (3)

If df is in fact a three-dimensional array you can do

df[1:20, 1:30, 20] <- df[1:20, 1:30, 27] + df[1:20, 1:30, 30]

BTW, df is the pdf of the F distribution in R, so not a good choice for a 
variable name.


On Fri, 10 Nov 2006, Sven C. Koehler wrote:

> Hello,
>
> currently I am using code, which basically works like this:
>
> |
> | for (i in 1:20) {
> |   for (j in 1:30) {
> |     df[i, j, 20] <- df[i,j,27] + df[i,j,30]
> |   }
> | }
>
> df is:  `data.frame: 360 obs. of  30 variables'
>
> Do you have any ideas whether I could get this code any faster?
>
> Regards,
>
> Sven C. Koehler
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
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 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list