[R] r programming help

Don MacQueen macq at llnl.gov
Wed Jun 22 17:56:20 CEST 2005


In addition to Peter's suggestion of converting 
to a matrix operation, here is a simple solution 
using naive R programming.

Using the 7th element of CYCLE.n as an example:
     replace this:
WET[1]*DRY[6]+WET[2]*DRY[5]+WET[3]*DRY[4]+WET[4]*DRY[3]+WET[5]*DRY[2]+WET[6]*DRY[1]
    with this:
sum(WET[1:6]*DRY[6:1])

which then suggests a loop.

nin <- length(WET)
CYCLE.n <- rep(NA,nin+1)
for (i in 1:nin) CYCLE.n[i+1] <- sum(WET[1:i]*DRY[i:1])


-Don

At 4:03 PM +0200 6/22/05, Peter Dalgaard wrote:
>Mohammad Ehsanul Karim <wildscop at yahoo.com> writes:
>
>>  Dear list,
>>
>>  Is there anyway i can make the following formula short
>>  by r-programming?
>>
>>  CYCLE.n<-c(NA,
>>  WET[1]*DRY[1],
>>  WET[1]*DRY[2]+WET[2]*DRY[1],
>>  WET[1]*DRY[3]+WET[2]*DRY[2]+WET[3]*DRY[1],
>....
>
>As far as I can see:
>
>z <- toeplitz(DRY)
>z[upper.tri(z)] <- 0
>c(NA, z %*% WET)
>
>or convolve() with suitable options, padding, and/or cutting (but
>beware, there could be devils in the details). convolve(WET,DRY,
>type="o") gives you about twice what you need, I believe.
>
>
>--
>    O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
>   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
>  (*) \(*) -- University of Copenhagen   Denmark          Ph: (+45) 35327918
>~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


-- 
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA




More information about the R-help mailing list