[R] how to speed up the inefficient code

jim holtman jholtman at gmail.com
Fri Mar 16 14:22:33 CET 2012


Try this -- use matrices instead of dataframes if you want speed:

> p<-data.frame(a=rnorm(10),b=rnorm(10),c=rnorm(10),d=rnorm(10))
> test<-data.frame(a=rnorm(1),b=rnorm(1),c=rnorm(1),d=rnorm(1))
>
> result<-list()
> for(i in 1:nrow(p)){
+ result[[i]]<-sum((p[i,]-test)^2)
+ }
>
> result_1<-unlist(result)
>
> p_1<-cbind(p,result_1)
>
> # don't use dataframes; use matrices
> pm <- as.matrix(p)
> testm <- unlist(test)
>
> result_2 <- colSums((t(pm) - testm)^2)
> p_2 <- cbind(pm, result_2)
> p_1
            a           b          c          d  result_1
1   1.4580819  0.03351271 -0.6414914 -0.4448870  2.464525
2  -0.7603889 -0.41833604 -1.3576672 -0.1230215  6.270578
3   1.3343855 -1.35613207  2.0560362 -1.0356030  3.592794
4   0.5974293  0.88175552 -1.4756855  0.1634320  6.707364
5  -0.8008295  0.37385864 -0.7822124 -0.4896094  4.712335
6  -0.5550789  0.07063761  0.9432475  1.0785456  4.868218
7   1.0641638 -1.52241836  0.3912154  0.1071740  2.165140
8   1.5619793 -0.30982169  1.2756762 -0.8522361  1.266879
9  -0.1049558 -1.66660559  0.5425563  0.3232156  3.279428
10 -0.1565198  0.59954267 -1.3878649  1.8615366 12.132722
> p_2
               a           b          c          d  result_2
 [1,]  1.4580819  0.03351271 -0.6414914 -0.4448870  2.464525
 [2,] -0.7603889 -0.41833604 -1.3576672 -0.1230215  6.270578
 [3,]  1.3343855 -1.35613207  2.0560362 -1.0356030  3.592794
 [4,]  0.5974293  0.88175552 -1.4756855  0.1634320  6.707364
 [5,] -0.8008295  0.37385864 -0.7822124 -0.4896094  4.712335
 [6,] -0.5550789  0.07063761  0.9432475  1.0785456  4.868218
 [7,]  1.0641638 -1.52241836  0.3912154  0.1071740  2.165140
 [8,]  1.5619793 -0.30982169  1.2756762 -0.8522361  1.266879
 [9,] -0.1049558 -1.66660559  0.5425563  0.3232156  3.279428
[10,] -0.1565198  0.59954267 -1.3878649  1.8615366 12.132722
>


On Fri, Mar 16, 2012 at 8:32 AM, mrzung <mrzung46 at gmail.com> wrote:
> hi,
>
> i'm really in trouble to simulate some experiment.
> that is, it takes too much time to process the following code.
>
>  following is short example,
>
> -------------------------------------------------------------------------------------------------------
>
> p<-data.frame(a=rnorm(10),b=rnorm(10),c=rnorm(10),d=rnorm(10))
> test<-data.frame(a=rnorm(1),b=rnorm(1),c=rnorm(1),d=rnorm(1))
>
> result<-list()
> for(i in 1:nrow(p)){
> result[[i]]<-sum((p[i,]-test)^2)
> }
>
> result_1<-unlist(result)
>
> p_1<-cbind(p,result_1)
>
> -------------------------------------------------------------------------------------------------------
>
> is there any efficient way to shorten the time and make same output?
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/how-to-speed-up-the-inefficient-code-tp4478046p4478046.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org 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.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.



More information about the R-help mailing list