[R] more on vectoring a simple for
Ray Brownrigg
ray at mcs.vuw.ac.nz
Tue Nov 26 00:24:14 CET 2002
> Excuse me, but I wasn't clear. It's a generic funcion, like
> f(beta) and g(beta)
>
> result<-rep(0,3)
> for(i in 1:3) result[i]<-sum(f(beta[-i])/g(beta[-i]))
>
> for example, f(beta)=log(abs(beta)) and g(beta)=beta^2:
> for(i in 1:3) result[i]<-sum(log(abs(beta[-i]))/(beta[-i]^2))
>
> How can I write this with no for?
>
The same principle applies, as long as f() and g() are vector functions.
i.e. sum(F(beta[-i])) = sum(F(beta)) - F(beta[i])
where F() = f()/g()
Viz:
> result <- rep(0, 3)
> beta <- 1:3
> for(i in 1:3) result[i] <- sum(log(abs(beta[-i]))/(beta[-i]^2))
> result
[1] 0.2953548 0.1220680 0.1732868
> sum(log(abs(beta))/(beta^2)) - log(abs(beta))/(beta^2)
[1] 0.2953548 0.1220680 0.1732868
>
Ray
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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