[R] more on vectoring a simple for

Deepayan Sarkar deepayan at stat.wisc.edu
Tue Nov 26 01:35:23 CET 2002


On Monday 25 November 2002 04:34 pm, Cezar Augusto de Freitas Anselmo wrote:
> 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 thing works, essentially. Assuming f, g are vectorized, 

foo <- log(abs(beta))/(beta^2)  ## in general f(beta)/g(beta)
result <- sum(foo) - foo



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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