[R] “For” calculation is so slow
Petr PIKAL
petr.pikal at precheza.cz
Tue May 22 12:15:03 CEST 2012
Hi
>
> Dear All,
>
> The function I wrote can run well with the small data, but with the
large
> data, the function runs very very slowly. How can I correct it? Thank
you
Your function does not run slowly, it does not run at all.
> l(10,20)
Error in l(10, 20) : object 's' not found
No s object found. I presume that also gx and gy will not be found.
And besides I am pretty sure that you probably does not need any loop at
all.
> very much. My function as below:
>
> a<-c(1:240)
> b<-c(1:240)
> l=function(a,b){
> v=0
> u=0
> uv=0
> v[1]=0
Why?
v<-0
v
[1] 0
v[1]<-0
v
[1] 0
So there is no difference between first "v" and second "v"
> u[1]=0
> uv[1]=0
> for (i in 1:(length(s)-1)){
>
v[i]<-((gx[[i]][b,(gx[[i]][a,1]+1)])-(gx[[i]][a,gx[[i]][a,1]+1]))/(gx[[i]]
> [a,gx[[i]][a,1]+1])
>
u[i]<-((gy[[i]][a,(gy[[i]][a,1]+1)])-(gy[[i]][b,gy[[i]][a,1]+1]))/(gy[[i]]
> [a,gy[[i]][a,1]+1])
> uv[i]<-v[i]+u[i]
> }
> w=0
> w=mean(uv)
Your function does not return anything. Here you can test it by yourself
on shortened version
l=function(a,b){
v=0
u=0
uv=0
v<-10
u<-20
uv<-v+u
w=0
w=mean(uv)
}
Regards
Petr
> }
> kk<-data.frame()
> for (a in 1:240){
> for (b in 1:240){
> if (a<b)
> kk[a,b]=l(a,b)
> else kk[a,b]=0
> }}
> max(kk)
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/For-
> calculation-is-so-slow-tp4630830.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.
More information about the R-help
mailing list