Re: [R] r programming help

From: Don MacQueen <macq_at_llnl.gov>
Date: Thu 23 Jun 2005 - 01:56:20 EST

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@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@biostat.ku.dk) FAX: (+45) 35327907
>
>______________________________________________
>R-help@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

______________________________________________
R-help@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
Received on Thu Jun 23 02:06:55 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:57 EST