[R] Writing Complex Formulas

Berend Hasselman bhh at xs4all.nl
Fri Jul 15 20:31:07 CEST 2011


warmstron1 wrote:
> 
>> for(j in 1:J)
> + {
> + Z <- dummy**B[j]
> + U <- (-dummy+1)**B[j]
> + }
>> Z
> 

I replaced ** with ^ and got the same results as you.

But why are you doing a for loop here?
At each iteration you are overwriting the previous results of Z and U and
retaining only the values obtained for j=J.
You could just as well do

    Z <- dummy^B[J]            # J  and not j
    U <- (-dummy+1)^B[J]     # same

I got the same results as you.



>> for(i in 1:4)
> + {
> + for(j in 1:3){
> + U[i,j]<-ifelse( U[i,j]>30,30,U[i,j])
> + }
> + U <- exp(U)
> 

The second closing } seems to be missing.
Why are you using ifelse elementwisely?
It is a vectorized function.
This is equivalent to what you are doing

U <- ifelse(U>30,30,U)

and still gives the same results.

Berend

--
View this message in context: http://r.789695.n4.nabble.com/Writing-Complex-Formulas-tp3638379p3670624.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list