[R] Loop removal possible?
Jan Goebel
jgoebel at diw.de
Mon Aug 21 18:02:20 CEST 2000
Thanks a lot for the help! So maybe you interested in my actual solution.
First i tried the suggested solutions (my memory problems come from
the length of the vectors. I have ca. 7000 observations and they will
be weighted to nearly 80.000.000 ... so i want to avoid expansion and
looping. I maybe haven't make this point so clear :-()
So this is what happens:
Berwin Turlach proposal in an single line (!). I do a round because of
float numbers:
> test<-as.vector(tapply(rep(post97[ok],round(w1110197[ok]))*(1:sum(round(w1110197[ok]))), rep(1:length(round(w1110197[ok])), round(w1110197[ok])), sum))
Error: heap memory (15360 Kb) exhausted [needed 54934 Kb more]
See "help(Memory)" on how to increase the heap size.
In Ben suggestion there will be no loop, but a integer overflow
and the solution is as slow as my:
> hilf.w <- function(w) {
w <- round(w)
e <- cumsum(w)
a <- e-(w-1)
k <- sapply(1:length(w),function(i)sum(a[i]:e[i]))
return(k)
}
> test<-hilf.w(w1110197[ok])
There were 50 warnings (use warnings() to see them)
> warnings()
Warning messages:
1: Integer overflow in sum(.); use sum(as.numeric(.))
# Jim Lemon solution brings me to the "right way" (imho):
> fungle<-function(xvec) {
return(xvec[2] + (xvec[2] + 1) - xvec[1])/2
}
> test<-apply(as.array(rbind(round(w1110197[ok]),cumsum(round(w1110197[ok])))),2,fungle)
# works quit well
So my final solution looks like this:
hilf3.w <- function(w) {
w <- round(w)
e <- cumsum(w)
a <- e-(w-1)
k <- (e*(e+1))/2 - (a*(a+1))/2 + a # good old Gaussian ;-)
return(k)
}
THANKS A LOT to all of your help and
good luck
Jan
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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