[R] r programming help III

2005-06-24 Thread Mohammad Ehsanul Karim
Dear List,

Is there any way we can make the following formula any
shorter by means of R programming:


# Transition Probabilities observed for 19 years
p01-0.4052863; p11-0.7309942 
# Now we try to find expected probabilities
# for number of wet days in a week
# as defined by Gabriel, Neumann (1962)
N-7
S-1; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1-p01))^a)*((p01/p11)^b))-p1N0
S-2; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1-p01))^a)*((p01/p11)^b))-p2N0
S-3; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1-p01))^a)*((p01/p11)^b))-p3N0
S-4; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1-p01))^a)*((p01/p11)^b))-p4N0
S-5; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1-p01))^a)*((p01/p11)^b))-p5N0
S-6; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1-p01))^a)*((p01/p11)^b))-p6N0
S-7; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1-p01))^a)*((p01/p11)^b))-p7N0
# we obtain probabilities for dry case
pN0-c(p11^N,p1N0,p2N0,p3N0,p4N0,p5N0,p6N0,p7N0)

S-0; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p01))^b)*((p01/p11)^a))-p0N1
S-1; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p01))^b)*((p01/p11)^a))-p1N1
S-2; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p01))^b)*((p01/p11)^a))-p2N1
S-3; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p01))^b)*((p01/p11)^a))-p3N1
S-4; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p01))^b)*((p01/p11)^a))-p4N1
S-5; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p01))^b)*((p01/p11)^a))-p5N1
S-6; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p01))^b)*((p01/p11)^a))-p6N1
# we obtain probabilities for wet case
pN1-c(p0N1,p1N1,p2N1,p3N1,p4N1,p5N1,p6N1, p11^N)
# Use of transition probabilities to get expected
probabilities
d-p11-p01
P-p01/(1-d)
pN-(1-P)*pN0+P*pN1 
# Expected Probabilities for number of wet days in a
week is pN
pN # gives the following output:


[1] 0.05164856 0.05126159 0.10424380 0.16317247
0.20470403 0.20621178 0.16104972
[8] 0.09170593




Any hint, help, support, references will be highly
appreciated.
Thank you for your time. 

--

Mohammad Ehsanul Karim 

Web: http://snipurl.com/ehsan 
ISRT, University of Dhaka, BD

__
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


Re: [R] r programming help III

2005-06-24 Thread Tuszynski, Jaroslaw W.
I am not sure if you can make this code shorter through R specific
programming, but You can through simple programming technique of using
functions:

pFunc = function (S, d, N, p01, p11)
{ 
  c1-N+1/2-abs(2*S-N-1/2+d); c-1:c1;
  a-ceiling((c-1+d)/2); b-ceiling((c-d)/2);
 
(p11^S)*((1-p01)^(N-S))*sum(choose((S-1+d),(b-1+d))*choose((N-S-d),a-d)*(((1
-p11)/(1-p01))^a)*((p01/p11)^b))
}

# Transition Probabilities observed for 19 years p01-0.4052863;
p11-0.7309942 # Now we try to find expected probabilities # for number of
wet days in a week # as defined by Gabriel, Neumann (1962)
N-7
p01=0.4052863
p11=0.7309942
pDry - function(S) pFunc(S,0,N,p01, p11)
pWet - function(S) pFunc(S,1,N,p01, p11)
# we obtain probabilities for dry case
pN0-c(p11^N, pDry(1), pDry(2), pDry(3), pDry(4), pDry(5), pDry(6), pDry(7)
)
# we obtain probabilities for wet case
pN1-c(pWet(0), pWet(1), pWet(2), pWet(3), pWet(4), pWet(5), pWet(6), p11^N)
# Use of transition probabilities to get expected probabilities
d-p11-p01
P-p01/(1-d)
pN-(1-P)*pN0+P*pN1

It is possible that you can find some ways of simplifying the pFunc.

Jarek
\===

 Jarek Tuszynski, PhD.   o / \ 
 Science Applications International Corporation  \__,|  
 (703) 676-4192  \
 [EMAIL PROTECTED] `\



-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Mohammad Ehsanul
Karim
Sent: Friday, June 24, 2005 5:16 AM
To: r help
Subject: [R] r programming help III

Dear List,

Is there any way we can make the following formula any shorter by means of R
programming:


# Transition Probabilities observed for 19 years p01-0.4052863;
p11-0.7309942 # Now we try to find expected probabilities # for number of
wet days in a week # as defined by Gabriel, Neumann (1962)
N-7
S-1; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1
-p01))^a)*((p01/p11)^b))-p1N0
S-2; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1
-p01))^a)*((p01/p11)^b))-p2N0
S-3; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1
-p01))^a)*((p01/p11)^b))-p3N0
S-4; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1
-p01))^a)*((p01/p11)^b))-p4N0
S-5; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1
-p01))^a)*((p01/p11)^b))-p5N0
S-6; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1
-p01))^a)*((p01/p11)^b))-p6N0
S-7; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1
-p01))^a)*((p01/p11)^b))-p7N0
# we obtain probabilities for dry case
pN0-c(p11^N,p1N0,p2N0,p3N0,p4N0,p5N0,p6N0,p7N0)

S-0; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p
01))^b)*((p01/p11)^a))-p0N1
S-1; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p
01))^b)*((p01/p11)^a))-p1N1
S-2; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p
01))^b)*((p01/p11)^a))-p2N1
S-3; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p
01))^b)*((p01/p11)^a))-p3N1
S-4; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p
01))^b)*((p01/p11)^a))-p4N1
S-5; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p
01))^b)*((p01/p11)^a))-p5N1
S-6; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
a-ceiling((c-1)/2); b-ceiling(c/2);
(p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p
01))^b)*((p01/p11)^a))-p6N1
# we obtain probabilities for wet case
pN1-c(p0N1,p1N1,p2N1,p3N1,p4N1,p5N1,p6N1, p11^N) # Use of transition
probabilities to get expected probabilities
d-p11-p01
P-p01/(1-d)
pN-(1-P)*pN0+P*pN1
# Expected Probabilities for number of wet days in a week is pN pN # gives
the following output:


[1] 0.05164856 0.05126159 0.10424380 0.16317247
0.20470403 0.20621178 0.16104972
[8] 0.09170593




Any hint, help, support, references will be highly
appreciated.
Thank you for your time

Re: [R] r programming help III

2005-06-24 Thread Gabor Grothendieck
Admittedly this is not much of an additional simplification but one other
thing that can be done is that pN0 and pN1 can be written in terms of 
sapply, e.g.

pN0-c(p11^N, sapply(1:7, pDry))


On 6/24/05, Tuszynski, Jaroslaw W. [EMAIL PROTECTED] wrote:
 I am not sure if you can make this code shorter through R specific
 programming, but You can through simple programming technique of using
 functions:
 
 pFunc = function (S, d, N, p01, p11)
 {
  c1-N+1/2-abs(2*S-N-1/2+d); c-1:c1;
  a-ceiling((c-1+d)/2); b-ceiling((c-d)/2);
 
 (p11^S)*((1-p01)^(N-S))*sum(choose((S-1+d),(b-1+d))*choose((N-S-d),a-d)*(((1
 -p11)/(1-p01))^a)*((p01/p11)^b))
 }
 
 # Transition Probabilities observed for 19 years p01-0.4052863;
 p11-0.7309942 # Now we try to find expected probabilities # for number of
 wet days in a week # as defined by Gabriel, Neumann (1962)
 N-7
 p01=0.4052863
 p11=0.7309942
 pDry - function(S) pFunc(S,0,N,p01, p11)
 pWet - function(S) pFunc(S,1,N,p01, p11)
 # we obtain probabilities for dry case
 pN0-c(p11^N, pDry(1), pDry(2), pDry(3), pDry(4), pDry(5), pDry(6), pDry(7)
 )
 # we obtain probabilities for wet case
 pN1-c(pWet(0), pWet(1), pWet(2), pWet(3), pWet(4), pWet(5), pWet(6), p11^N)
 # Use of transition probabilities to get expected probabilities
 d-p11-p01
 P-p01/(1-d)
 pN-(1-P)*pN0+P*pN1
 
 It is possible that you can find some ways of simplifying the pFunc.
 
 Jarek
 \===
 
  Jarek Tuszynski, PhD.   o / \
  Science Applications International Corporation  \__,|
  (703) 676-4192  \
  [EMAIL PROTECTED] `\
 
 
 
 -Original Message-
 From: [EMAIL PROTECTED]
 [mailto:[EMAIL PROTECTED] On Behalf Of Mohammad Ehsanul
 Karim
 Sent: Friday, June 24, 2005 5:16 AM
 To: r help
 Subject: [R] r programming help III
 
 Dear List,
 
 Is there any way we can make the following formula any shorter by means of R
 programming:
 
 
 # Transition Probabilities observed for 19 years p01-0.4052863;
 p11-0.7309942 # Now we try to find expected probabilities # for number of
 wet days in a week # as defined by Gabriel, Neumann (1962)
 N-7
 S-1; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
 a-ceiling((c-1)/2); b-ceiling(c/2);
 (p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1
 -p01))^a)*((p01/p11)^b))-p1N0
 S-2; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
 a-ceiling((c-1)/2); b-ceiling(c/2);
 (p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1
 -p01))^a)*((p01/p11)^b))-p2N0
 S-3; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
 a-ceiling((c-1)/2); b-ceiling(c/2);
 (p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1
 -p01))^a)*((p01/p11)^b))-p3N0
 S-4; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
 a-ceiling((c-1)/2); b-ceiling(c/2);
 (p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1
 -p01))^a)*((p01/p11)^b))-p4N0
 S-5; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
 a-ceiling((c-1)/2); b-ceiling(c/2);
 (p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1
 -p01))^a)*((p01/p11)^b))-p5N0
 S-6; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
 a-ceiling((c-1)/2); b-ceiling(c/2);
 (p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1
 -p01))^a)*((p01/p11)^b))-p6N0
 S-7; c0-N+1/2-abs(2*S-N-1/2);c-1:c0;
 a-ceiling((c-1)/2); b-ceiling(c/2);
 (p11^S)*((1-p01)^(N-S))*sum(choose((S-1),(b-1))*choose((N-S),a)*(((1-p11)/(1
 -p01))^a)*((p01/p11)^b))-p7N0
 # we obtain probabilities for dry case
 pN0-c(p11^N,p1N0,p2N0,p3N0,p4N0,p5N0,p6N0,p7N0)
 
 S-0; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
 a-ceiling((c-1)/2); b-ceiling(c/2);
 (p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p
 01))^b)*((p01/p11)^a))-p0N1
 S-1; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
 a-ceiling((c-1)/2); b-ceiling(c/2);
 (p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p
 01))^b)*((p01/p11)^a))-p1N1
 S-2; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
 a-ceiling((c-1)/2); b-ceiling(c/2);
 (p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p
 01))^b)*((p01/p11)^a))-p2N1
 S-3; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
 a-ceiling((c-1)/2); b-ceiling(c/2);
 (p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p
 01))^b)*((p01/p11)^a))-p3N1
 S-4; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
 a-ceiling((c-1)/2); b-ceiling(c/2);
 (p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p
 01))^b)*((p01/p11)^a))-p4N1
 S-5; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
 a-ceiling((c-1)/2); b-ceiling(c/2);
 (p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p
 01))^b)*((p01/p11)^a))-p5N1
 S-6; c1-N+1/2-abs(2*S-N+1/2);c-1:c1;
 a-ceiling((c-1)/2); b-ceiling(c/2);
 (p11^S)*((1-p01)^(N-S))*sum(choose(S,a)*choose((N-S-1),(b-1))*(((1-p11)/(1-p
 01))^b)*((p01/p11)^a))-p6N1
 # we obtain probabilities for wet case
 pN1-c(p0N1,p1N1,p2N1,p3N1,p4N1,p5N1,p6N1, p11^N) # Use of transition
 probabilities to get expected

[R] r programming help II

2005-06-23 Thread Mohammad Ehsanul Karim
Dear List,

Suppose we have a variable K.JUN defined as (with
1=wet, 0=dry):

K.JUN1984 = c(1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) 
K.JUN1985 = c(0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1) 
K.JUN1986 = c(0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1)
K.JUN1987 = c(0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0,
1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0)
K.JUN1988 = c(1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0)
K.JUN1989 = c(0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1,
1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1)
K.JUN1990 = c(1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0)
K.JUN1991 = c(0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0)
K.JUN1992 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0)
K.JUN1993 = c(0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0,
0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0)
K.JUN1994 = c(0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1,
1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1)
K.JUN1995 = c(0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1,
1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0)
K.JUN1996 = c(0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0,
0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1)
K.JUN1997 = c(0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1,
1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1)
K.JUN1998 = c(1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0,
1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0)
K.JUN1999 = c(0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0,
0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0)
K.JUN2000 = c(1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1,
1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0)
K.JUN2001 = c(1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1)
K.JUN2002 = c(1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1,
1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1)

K.JUN-c(K.JUN1984,K.JUN1985,K.JUN1986,K.JUN1987,K.JUN1988,K.JUN1989,K.JUN1990,K.JUN1991,K.JUN1992,K.JUN1993,K.JUN1994,K.JUN1995,K.JUN1996,K.JUN1997,K.JUN1998,K.JUN1999,K.JUN2000,K.JUN2001,K.JUN2002)

Our motivation is to count number of wet days (1's) in
each weeks. But counting number of wet days for entire
K.JUN will not do.
Thus in r console,

 k-0;j-0;i-(1:7)+30*j+7*k;K.JUN[i];rle(K.JUN[i])
[1] 1 0 1 1 1 1 1
Run Length Encoding
  lengths: int [1:3] 1 1 5
  values : num [1:3] 1 0 1

where k=0,1,2,3 for each j=0 to 18 (k indicating weeks
of any June, and j indicates years 1984-2002
respectively).

Now we need to sum the run 'lengths' corresponding to
each 'values' 1 (that is 'lengths' of each 0
'values' need to be excluded) for all k=0,1,2,3 for
each j=0 to 18 (for example, for k-0;j-0 we find
'lengths' 1 and 5 for 'values' 1 in the above: then
we sum it as
sum(rle(K.JUN[(1:7)+30*0+7*0])$lengths[c(1,3)])
manually).

Doing so for all k=0,1,2,3 for each j=0 to 18 manually
like this
k-0;j-0;i-(1:7)+30*j+7*k;K.JUN[i];rle(K.JUN[i])
k-1;j-0;i-(1:7)+30*j+7*k;K.JUN[i];rle(K.JUN[i])
...
k-3;j-18;i-(1:7)+30*j+7*k;K.JUN[i];rle(K.JUN[i])
we observe the run 'lengths' corresponding to all 1
'values' and sum them under a new variable JUN.w as
follows (a cumbersome process obviously):


JUN.w-c(sum(rle(K.JUN[(1:7)+30*0+7*0])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*0+7*1])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*0+7*2])$lengths[1]),sum(rle(K.JUN[(1:7)+30*0+7*3])$lengths[1]),
sum(rle(K.JUN[(1:7)+30*1+7*0])$lengths[c(2,4,6)]),sum(rle(K.JUN[(1:7)+30*1+7*1])$lengths[c(2,4)]),sum(rle(K.JUN[(1:7)+30*1+7*2])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*1+7*3])$lengths[2]),
sum(rle(K.JUN[(1:7)+30*2+7*0])$lengths[2]),sum(rle(K.JUN[(1:7)+30*2+7*1])$lengths[2]),sum(rle(K.JUN[(1:7)+30*2+7*2])$lengths[2]),sum(rle(K.JUN[(1:7)+30*2+7*3])$lengths[1]),
sum(rle(K.JUN[(1:7)+30*3+7*0])$lengths[2]),sum(rle(K.JUN[(1:7)+30*3+7*1])$lengths[c(1,3,5)]),sum(rle(K.JUN[(1:7)+30*3+7*2])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*3+7*3])$lengths[1]),
sum(rle(K.JUN[(1:7)+30*4+7*0])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*4+7*1])$lengths[1]),sum(rle(K.JUN[(1:7)+30*4+7*2])$lengths[1]),sum(rle(K.JUN[(1:7)+30*4+7*3])$lengths[1]),
sum(rle(K.JUN[(1:7)+30*5+7*0])$lengths[c(2,4)]),sum(rle(K.JUN[(1:7)+30*5+7*1])$lengths[c(2,4)]),sum(rle(K.JUN[(1:7)+30*5+7*2])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*5+7*3])$lengths[1]),
sum(rle(K.JUN[(1:7)+30*6+7*0])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*6+7*1])$lengths[c(2,4)]),sum(rle(K.JUN[(1:7)+30*6+7*2])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*6+7*3])$lengths[c(1,3)]),
sum(rle(K.JUN[(1:7)+30*7+7*0])$lengths[c(2,4)]),sum(rle(K.JUN[(1:7)+30*7+7*1])$lengths[1]),sum(rle(K.JUN[(1:7)+30*7+7*2])$lengths[1]),sum(rle(K.JUN[(1:7)+30*7+7*3])$lengths[2]),
0,sum(rle(K.JUN[(1:7)+30*8+7*1])$lengths[2]),sum(rle(K.JUN[(1:7)+30*8+7*2])$lengths[c(2,4)]),sum(rle(K.JUN[(1:7)+30*8+7*3])$lengths[1]),

Re: [R] r programming help II

2005-06-23 Thread Gabor Grothendieck
On 6/24/05, Mohammad Ehsanul Karim [EMAIL PROTECTED] wrote:
 Dear List,
 
 Suppose we have a variable K.JUN defined as (with
 1=wet, 0=dry):
 
 K.JUN1984 = c(1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1,
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
 K.JUN1985 = c(0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1)
 K.JUN1986 = c(0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1)
 K.JUN1987 = c(0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0,
 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0)
 K.JUN1988 = c(1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0)
 K.JUN1989 = c(0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1,
 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1)
 K.JUN1990 = c(1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0)
 K.JUN1991 = c(0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0)
 K.JUN1992 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0)
 K.JUN1993 = c(0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0,
 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0)
 K.JUN1994 = c(0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1,
 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1)
 K.JUN1995 = c(0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1,
 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0)
 K.JUN1996 = c(0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0,
 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1)
 K.JUN1997 = c(0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1,
 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1)
 K.JUN1998 = c(1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0,
 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0)
 K.JUN1999 = c(0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0,
 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0)
 K.JUN2000 = c(1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1,
 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0)
 K.JUN2001 = c(1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1,
 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1)
 K.JUN2002 = c(1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1,
 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1)
 
 K.JUN-c(K.JUN1984,K.JUN1985,K.JUN1986,K.JUN1987,K.JUN1988,K.JUN1989,K.JUN1990,K.JUN1991,K.JUN1992,K.JUN1993,K.JUN1994,K.JUN1995,K.JUN1996,K.JUN1997,K.JUN1998,K.JUN1999,K.JUN2000,K.JUN2001,K.JUN2002)
 
 Our motivation is to count number of wet days (1's) in
 each weeks. But counting number of wet days for entire
 K.JUN will not do.
 Thus in r console,
 
  k-0;j-0;i-(1:7)+30*j+7*k;K.JUN[i];rle(K.JUN[i])
 [1] 1 0 1 1 1 1 1
 Run Length Encoding
  lengths: int [1:3] 1 1 5
  values : num [1:3] 1 0 1
 
 where k=0,1,2,3 for each j=0 to 18 (k indicating weeks
 of any June, and j indicates years 1984-2002
 respectively).
 
 Now we need to sum the run 'lengths' corresponding to
 each 'values' 1 (that is 'lengths' of each 0
 'values' need to be excluded) for all k=0,1,2,3 for
 each j=0 to 18 (for example, for k-0;j-0 we find
 'lengths' 1 and 5 for 'values' 1 in the above: then
 we sum it as
 sum(rle(K.JUN[(1:7)+30*0+7*0])$lengths[c(1,3)])
 manually).
 
 Doing so for all k=0,1,2,3 for each j=0 to 18 manually
 like this
 k-0;j-0;i-(1:7)+30*j+7*k;K.JUN[i];rle(K.JUN[i])
 k-1;j-0;i-(1:7)+30*j+7*k;K.JUN[i];rle(K.JUN[i])
 ...
 k-3;j-18;i-(1:7)+30*j+7*k;K.JUN[i];rle(K.JUN[i])
 we observe the run 'lengths' corresponding to all 1
 'values' and sum them under a new variable JUN.w as
 follows (a cumbersome process obviously):
 
 
 JUN.w-c(sum(rle(K.JUN[(1:7)+30*0+7*0])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*0+7*1])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*0+7*2])$lengths[1]),sum(rle(K.JUN[(1:7)+30*0+7*3])$lengths[1]),
 sum(rle(K.JUN[(1:7)+30*1+7*0])$lengths[c(2,4,6)]),sum(rle(K.JUN[(1:7)+30*1+7*1])$lengths[c(2,4)]),sum(rle(K.JUN[(1:7)+30*1+7*2])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*1+7*3])$lengths[2]),
 sum(rle(K.JUN[(1:7)+30*2+7*0])$lengths[2]),sum(rle(K.JUN[(1:7)+30*2+7*1])$lengths[2]),sum(rle(K.JUN[(1:7)+30*2+7*2])$lengths[2]),sum(rle(K.JUN[(1:7)+30*2+7*3])$lengths[1]),
 sum(rle(K.JUN[(1:7)+30*3+7*0])$lengths[2]),sum(rle(K.JUN[(1:7)+30*3+7*1])$lengths[c(1,3,5)]),sum(rle(K.JUN[(1:7)+30*3+7*2])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*3+7*3])$lengths[1]),
 sum(rle(K.JUN[(1:7)+30*4+7*0])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*4+7*1])$lengths[1]),sum(rle(K.JUN[(1:7)+30*4+7*2])$lengths[1]),sum(rle(K.JUN[(1:7)+30*4+7*3])$lengths[1]),
 sum(rle(K.JUN[(1:7)+30*5+7*0])$lengths[c(2,4)]),sum(rle(K.JUN[(1:7)+30*5+7*1])$lengths[c(2,4)]),sum(rle(K.JUN[(1:7)+30*5+7*2])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*5+7*3])$lengths[1]),
 sum(rle(K.JUN[(1:7)+30*6+7*0])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*6+7*1])$lengths[c(2,4)]),sum(rle(K.JUN[(1:7)+30*6+7*2])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*6+7*3])$lengths[c(1,3)]),
 sum(rle(K.JUN[(1:7)+30*7+7*0])$lengths[c(2,4)]),sum(rle(K.JUN[(1:7)+30*7+7*1])$lengths[1]),sum(rle(K.JUN[(1:7)+30*7+7*2])$lengths[1]),sum(rle(K.JUN[(1:7)+30*7+7*3])$lengths[2]),
 

[R] r programming help

2005-06-22 Thread Mohammad Ehsanul Karim
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],
WET[1]*DRY[4]+WET[2]*DRY[3]+WET[3]*DRY[2]+WET[4]*DRY[1],
WET[1]*DRY[5]+WET[2]*DRY[4]+WET[3]*DRY[3]+WET[4]*DRY[2]+WET[5]*DRY[1],
WET[1]*DRY[6]+WET[2]*DRY[5]+WET[3]*DRY[4]+WET[4]*DRY[3]+WET[5]*DRY[2]+WET[6]*DRY[1],
WET[1]*DRY[7]+WET[2]*DRY[6]+WET[3]*DRY[5]+WET[4]*DRY[4]+WET[5]*DRY[3]+WET[6]*DRY[2]+WET[7]*DRY[1],
WET[1]*DRY[8]+WET[2]*DRY[7]+WET[3]*DRY[6]+WET[4]*DRY[5]+WET[5]*DRY[4]+WET[6]*DRY[3]+WET[7]*DRY[2]+WET[8]*DRY[1],
WET[1]*DRY[9]+WET[2]*DRY[8]+WET[3]*DRY[7]+WET[4]*DRY[6]+WET[5]*DRY[5]+WET[6]*DRY[4]+WET[7]*DRY[3]+WET[8]*DRY[2]+WET[9]*DRY[1],
WET[1]*DRY[10]+WET[2]*DRY[9]+WET[3]*DRY[8]+WET[4]*DRY[7]+WET[5]*DRY[6]+WET[6]*DRY[5]+WET[7]*DRY[4]+WET[8]*DRY[3]+WET[9]*DRY[2]+WET[10]*DRY[1],
WET[1]*DRY[11]+WET[2]*DRY[10]+WET[3]*DRY[9]+WET[4]*DRY[8]+WET[5]*DRY[7]+WET[6]*DRY[6]+WET[7]*DRY[5]+WET[8]*DRY[4]+WET[9]*DRY[3]+WET[10]*DRY[2]+WET[11]*DRY[1],
WET[1]*DRY[12]+WET[2]*DRY[11]+WET[3]*DRY[10]+WET[4]*DRY[9]+WET[5]*DRY[8]+WET[6]*DRY[7]+WET[7]*DRY[6]+WET[8]*DRY[5]+WET[9]*DRY[4]+WET[10]*DRY[3]+WET[11]*DRY[2]+WET[12]*DRY[1],
WET[1]*DRY[13]+WET[2]*DRY[12]+WET[3]*DRY[11]+WET[4]*DRY[10]+WET[5]*DRY[9]+WET[6]*DRY[8]+WET[7]*DRY[7]+WET[8]*DRY[6]+WET[9]*DRY[5]+WET[10]*DRY[4]+WET[11]*DRY[3]+WET[12]*DRY[2]+WET[13]*DRY[1],
WET[1]*DRY[14]+WET[2]*DRY[13]+WET[3]*DRY[12]+WET[4]*DRY[11]+WET[5]*DRY[10]+WET[6]*DRY[9]+WET[7]*DRY[8]+WET[8]*DRY[7]+WET[9]*DRY[6]+WET[10]*DRY[5]+WET[11]*DRY[4]+WET[12]*DRY[3]+WET[13]*DRY[2]+WET[14]*DRY[1],
WET[1]*DRY[15]+WET[2]*DRY[14]+WET[3]*DRY[13]+WET[4]*DRY[12]+WET[5]*DRY[11]+WET[6]*DRY[10]+WET[7]*DRY[9]+WET[8]*DRY[8]+WET[9]*DRY[7]+WET[10]*DRY[6]+WET[11]*DRY[5]+WET[12]*DRY[4]+WET[13]*DRY[3]+WET[14]*DRY[2]+WET[15]*DRY[1],
WET[1]*DRY[16]+WET[2]*DRY[15]+WET[3]*DRY[14]+WET[4]*DRY[13]+WET[5]*DRY[12]+WET[6]*DRY[11]+WET[7]*DRY[10]+WET[8]*DRY[9]+WET[9]*DRY[8]+WET[10]*DRY[7]+WET[11]*DRY[6]+WET[12]*DRY[5]+WET[13]*DRY[4]+WET[14]*DRY[3]+WET[15]*DRY[2]+WET[16]*DRY[1],
WET[1]*DRY[17]+WET[2]*DRY[16]+WET[3]*DRY[15]+WET[4]*DRY[14]+WET[5]*DRY[13]+WET[6]*DRY[12]+WET[7]*DRY[11]+WET[8]*DRY[10]+WET[9]*DRY[9]+WET[10]*DRY[8]+WET[11]*DRY[7]+WET[12]*DRY[6]+WET[13]*DRY[5]+WET[14]*DRY[4]+WET[15]*DRY[3]+WET[16]*DRY[2]+WET[17]*DRY[1],
WET[1]*DRY[18]+WET[2]*DRY[17]+WET[3]*DRY[16]+WET[4]*DRY[15]+WET[5]*DRY[14]+WET[6]*DRY[13]+WET[7]*DRY[12]+WET[8]*DRY[11]+WET[9]*DRY[10]+WET[10]*DRY[9]+WET[11]*DRY[8]+WET[12]*DRY[7]+WET[13]*DRY[6]+WET[14]*DRY[5]+WET[15]*DRY[4]+WET[16]*DRY[3]+WET[17]*DRY[2]+WET[18]*DRY[1],
WET[1]*DRY[19]+WET[2]*DRY[18]+WET[3]*DRY[17]+WET[4]*DRY[16]+WET[5]*DRY[15]+WET[6]*DRY[15]+WET[7]*DRY[13]+WET[8]*DRY[12]+WET[9]*DRY[11]+WET[10]*DRY[10]+WET[11]*DRY[9]+WET[12]*DRY[8]+WET[13]*DRY[7]+WET[14]*DRY[6]+WET[15]*DRY[5]+WET[16]*DRY[4]+WET[17]*DRY[3]+WET[18]*DRY[2]+WET[19]*DRY[1],
WET[1]*DRY[20]+WET[2]*DRY[19]+WET[3]*DRY[18]+WET[4]*DRY[17]+WET[5]*DRY[16]+WET[6]*DRY[15]+WET[7]*DRY[14]+WET[8]*DRY[13]+WET[9]*DRY[12]+WET[10]*DRY[11]+WET[11]*DRY[10]+WET[12]*DRY[9]+WET[13]*DRY[8]+WET[14]*DRY[7]+WET[15]*DRY[6]+WET[16]*DRY[5]+WET[17]*DRY[4]+WET[18]*DRY[3]+WET[19]*DRY[2]+WET[20]*DRY[1],
WET[1]*DRY[21]+WET[2]*DRY[20]+WET[3]*DRY[19]+WET[4]*DRY[18]+WET[5]*DRY[17]+WET[6]*DRY[16]+WET[7]*DRY[15]+WET[8]*DRY[14]+WET[9]*DRY[13]+WET[10]*DRY[12]+WET[11]*DRY[11]+WET[12]*DRY[10]+WET[13]*DRY[9]+WET[14]*DRY[8]+WET[15]*DRY[7]+WET[16]*DRY[6]+WET[17]*DRY[5]+WET[18]*DRY[4]+WET[19]*DRY[3]+WET[20]*DRY[2]+WET[21]*DRY[1],
WET[1]*DRY[22]+WET[2]*DRY[21]+WET[3]*DRY[20]+WET[4]*DRY[19]+WET[5]*DRY[18]+WET[6]*DRY[17]+WET[7]*DRY[16]+WET[8]*DRY[15]+WET[9]*DRY[14]+WET[10]*DRY[13]+WET[11]*DRY[12]+WET[12]*DRY[11]+WET[13]*DRY[10]+WET[14]*DRY[9]+WET[15]*DRY[8]+WET[16]*DRY[7]+WET[17]*DRY[6]+WET[18]*DRY[5]+WET[19]*DRY[4]+WET[20]*DRY[3]+WET[21]*DRY[2]+WET[22]*DRY[1],
WET[1]*DRY[23]+WET[2]*DRY[22]+WET[3]*DRY[21]+WET[4]*DRY[20]+WET[5]*DRY[19]+WET[6]*DRY[18]+WET[7]*DRY[17]+WET[8]*DRY[16]+WET[9]*DRY[15]+WET[10]*DRY[14]+WET[11]*DRY[13]+WET[12]*DRY[12]+WET[13]*DRY[11]+WET[14]*DRY[10]+WET[15]*DRY[9]+WET[16]*DRY[8]+WET[17]*DRY[7]+WET[18]*DRY[6]+WET[19]*DRY[5]+WET[20]*DRY[4]+WET[21]*DRY[3]+WET[22]*DRY[2]+WET[23]*DRY[1],
WET[1]*DRY[24]+WET[2]*DRY[23]+WET[3]*DRY[22]+WET[4]*DRY[21]+WET[5]*DRY[20]+WET[6]*DRY[19]+WET[7]*DRY[18]+WET[8]*DRY[17]+WET[9]*DRY[16]+WET[10]*DRY[15]+WET[11]*DRY[14]+WET[12]*DRY[13]+WET[13]*DRY[12]+WET[14]*DRY[11]+WET[15]*DRY[10]+WET[16]*DRY[9]+WET[17]*DRY[8]+WET[18]*DRY[7]+WET[19]*DRY[6]+WET[20]*DRY[5]+WET[21]*DRY[4]+WET[22]*DRY[3]+WET[23]*DRY[2]+WET[24]*DRY[1],
WET[1]*DRY[25]+WET[2]*DRY[24]+WET[3]*DRY[23]+WET[4]*DRY[22]+WET[5]*DRY[21]+WET[6]*DRY[20]+WET[7]*DRY[19]+WET[8]*DRY[18]+WET[9]*DRY[17]+WET[10]*DRY[16]+WET[11]*DRY[15]+WET[12]*DRY[14]+WET[13]*DRY[13]+WET[14]*DRY[12]+WET[15]*DRY[11]+WET[16]*DRY[10]+WET[17]*DRY[9]+WET[18]*DRY[8]+WET[19]*DRY[7]+WET[20]*DRY[6]+WET[21]*DRY[5]+WET[22]*DRY[4]+WET[23]*DRY[3]+WET[24]*DRY[2]+WET[25]*DRY[1],

Re: [R] r programming help

2005-06-22 Thread Peter Dalgaard
Mohammad Ehsanul Karim [EMAIL PROTECTED] 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
~~ - ([EMAIL PROTECTED])  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


Re: [R] r programming help

2005-06-22 Thread Peter Wolf
Try:

  DRY-c(2,5,3,7,11)
  WET-(1:5)*10
  print(filter(c(rep(0,length(WET)),DRY),WET))

Time Series:
Start = 1
End = 10
Frequency = 1
[1]  NA  NA   0  20  90 190 360 640  NA  NA

  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],
  WET[1]*DRY[4]+WET[2]*DRY[3]+WET[3]*DRY[2]+WET[4]*DRY[1],
  WET[1]*DRY[5]+WET[2]*DRY[4]+WET[3]*DRY[3]+WET[4]*DRY[2]+WET[5]*DRY[1])
  print(CYCLE.n)
[1]  NA  20  90 190 360 640

Peter Wolf

Mohammad Ehsanul Karim wrote:

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],
WET[1]*DRY[4]+WET[2]*DRY[3]+WET[3]*DRY[2]+WET[4]*DRY[1],
WET[1]*DRY[5]+WET[2]*DRY[4]+WET[3]*DRY[3]+WET[4]*DRY[2]+WET[5]*DRY[1],
WET[1]*DRY[6]+WET[2]*DRY[5]+WET[3]*DRY[4]+WET[4]*DRY[3]+WET[5]*DRY[2]+WET[6]*DRY[1],
WET[1]*DRY[7]+WET[2]*DRY[6]+WET[3]*DRY[5]+WET[4]*DRY[4]+WET[5]*DRY[3]+WET[6]*DRY[2]+WET[7]*DRY[1],
WET[1]*DRY[8]+WET[2]*DRY[7]+WET[3]*DRY[6]+WET[4]*DRY[5]+WET[5]*DRY[4]+WET[6]*DRY[3]+WET[7]*DRY[2]+WET[8]*DRY[1],
WET[1]*DRY[9]+WET[2]*DRY[8]+WET[3]*DRY[7]+WET[4]*DRY[6]+WET[5]*DRY[5]+WET[6]*DRY[4]+WET[7]*DRY[3]+WET[8]*DRY[2]+WET[9]*DRY[1],
WET[1]*DRY[10]+WET[2]*DRY[9]+WET[3]*DRY[8]+WET[4]*DRY[7]+WET[5]*DRY[6]+WET[6]*DRY[5]+WET[7]*DRY[4]+WET[8]*DRY[3]+WET[9]*DRY[2]+WET[10]*DRY[1],
WET[1]*DRY[11]+WET[2]*DRY[10]+WET[3]*DRY[9]+WET[4]*DRY[8]+WET[5]*DRY[7]+WET[6]*DRY[6]+WET[7]*DRY[5]+WET[8]*DRY[4]+WET[9]*DRY[3]+WET[10]*DRY[2]+WET[11]*DRY[1],
WET[1]*DRY[12]+WET[2]*DRY[11]+WET[3]*DRY[10]+WET[4]*DRY[9]+WET[5]*DRY[8]+WET[6]*DRY[7]+WET[7]*DRY[6]+WET[8]*DRY[5]+WET[9]*DRY[4]+WET[10]*DRY[3]+WET[11]*DRY[2]+WET[12]*DRY[1],
WET[1]*DRY[13]+WET[2]*DRY[12]+WET[3]*DRY[11]+WET[4]*DRY[10]+WET[5]*DRY[9]+WET[6]*DRY[8]+WET[7]*DRY[7]+WET[8]*DRY[6]+WET[9]*DRY[5]+WET[10]*DRY[4]+WET[11]*DRY[3]+WET[12]*DRY[2]+WET[13]*DRY[1],
WET[1]*DRY[14]+WET[2]*DRY[13]+WET[3]*DRY[12]+WET[4]*DRY[11]+WET[5]*DRY[10]+WET[6]*DRY[9]+WET[7]*DRY[8]+WET[8]*DRY[7]+WET[9]*DRY[6]+WET[10]*DRY[5]+WET[11]*DRY[4]+WET[12]*DRY[3]+WET[13]*DRY[2]+WET[14]*DRY[1],
WET[1]*DRY[15]+WET[2]*DRY[14]+WET[3]*DRY[13]+WET[4]*DRY[12]+WET[5]*DRY[11]+WET[6]*DRY[10]+WET[7]*DRY[9]+WET[8]*DRY[8]+WET[9]*DRY[7]+WET[10]*DRY[6]+WET[11]*DRY[5]+WET[12]*DRY[4]+WET[13]*DRY[3]+WET[14]*DRY[2]+WET[15]*DRY[1],
WET[1]*DRY[16]+WET[2]*DRY[15]+WET[3]*DRY[14]+WET[4]*DRY[13]+WET[5]*DRY[12]+WET[6]*DRY[11]+WET[7]*DRY[10]+WET[8]*DRY[9]+WET[9]*DRY[8]+WET[10]*DRY[7]+WET[11]*DRY[6]+WET[12]*DRY[5]+WET[13]*DRY[4]+WET[14]*DRY[3]+WET[15]*DRY[2]+WET[16]*DRY[1],
WET[1]*DRY[17]+WET[2]*DRY[16]+WET[3]*DRY[15]+WET[4]*DRY[14]+WET[5]*DRY[13]+WET[6]*DRY[12]+WET[7]*DRY[11]+WET[8]*DRY[10]+WET[9]*DRY[9]+WET[10]*DRY[8]+WET[11]*DRY[7]+WET[12]*DRY[6]+WET[13]*DRY[5]+WET[14]*DRY[4]+WET[15]*DRY[3]+WET[16]*DRY[2]+WET[17]*DRY[1],
WET[1]*DRY[18]+WET[2]*DRY[17]+WET[3]*DRY[16]+WET[4]*DRY[15]+WET[5]*DRY[14]+WET[6]*DRY[13]+WET[7]*DRY[12]+WET[8]*DRY[11]+WET[9]*DRY[10]+WET[10]*DRY[9]+WET[11]*DRY[8]+WET[12]*DRY[7]+WET[13]*DRY[6]+WET[14]*DRY[5]+WET[15]*DRY[4]+WET[16]*DRY[3]+WET[17]*DRY[2]+WET[18]*DRY[1],
WET[1]*DRY[19]+WET[2]*DRY[18]+WET[3]*DRY[17]+WET[4]*DRY[16]+WET[5]*DRY[15]+WET[6]*DRY[15]+WET[7]*DRY[13]+WET[8]*DRY[12]+WET[9]*DRY[11]+WET[10]*DRY[10]+WET[11]*DRY[9]+WET[12]*DRY[8]+WET[13]*DRY[7]+WET[14]*DRY[6]+WET[15]*DRY[5]+WET[16]*DRY[4]+WET[17]*DRY[3]+WET[18]*DRY[2]+WET[19]*DRY[1],
WET[1]*DRY[20]+WET[2]*DRY[19]+WET[3]*DRY[18]+WET[4]*DRY[17]+WET[5]*DRY[16]+WET[6]*DRY[15]+WET[7]*DRY[14]+WET[8]*DRY[13]+WET[9]*DRY[12]+WET[10]*DRY[11]+WET[11]*DRY[10]+WET[12]*DRY[9]+WET[13]*DRY[8]+WET[14]*DRY[7]+WET[15]*DRY[6]+WET[16]*DRY[5]+WET[17]*DRY[4]+WET[18]*DRY[3]+WET[19]*DRY[2]+WET[20]*DRY[1],
WET[1]*DRY[21]+WET[2]*DRY[20]+WET[3]*DRY[19]+WET[4]*DRY[18]+WET[5]*DRY[17]+WET[6]*DRY[16]+WET[7]*DRY[15]+WET[8]*DRY[14]+WET[9]*DRY[13]+WET[10]*DRY[12]+WET[11]*DRY[11]+WET[12]*DRY[10]+WET[13]*DRY[9]+WET[14]*DRY[8]+WET[15]*DRY[7]+WET[16]*DRY[6]+WET[17]*DRY[5]+WET[18]*DRY[4]+WET[19]*DRY[3]+WET[20]*DRY[2]+WET[21]*DRY[1],
WET[1]*DRY[22]+WET[2]*DRY[21]+WET[3]*DRY[20]+WET[4]*DRY[19]+WET[5]*DRY[18]+WET[6]*DRY[17]+WET[7]*DRY[16]+WET[8]*DRY[15]+WET[9]*DRY[14]+WET[10]*DRY[13]+WET[11]*DRY[12]+WET[12]*DRY[11]+WET[13]*DRY[10]+WET[14]*DRY[9]+WET[15]*DRY[8]+WET[16]*DRY[7]+WET[17]*DRY[6]+WET[18]*DRY[5]+WET[19]*DRY[4]+WET[20]*DRY[3]+WET[21]*DRY[2]+WET[22]*DRY[1],
WET[1]*DRY[23]+WET[2]*DRY[22]+WET[3]*DRY[21]+WET[4]*DRY[20]+WET[5]*DRY[19]+WET[6]*DRY[18]+WET[7]*DRY[17]+WET[8]*DRY[16]+WET[9]*DRY[15]+WET[10]*DRY[14]+WET[11]*DRY[13]+WET[12]*DRY[12]+WET[13]*DRY[11]+WET[14]*DRY[10]+WET[15]*DRY[9]+WET[16]*DRY[8]+WET[17]*DRY[7]+WET[18]*DRY[6]+WET[19]*DRY[5]+WET[20]*DRY[4]+WET[21]*DRY[3]+WET[22]*DRY[2]+WET[23]*DRY[1],

Re: [R] r programming help

2005-06-22 Thread Don MacQueen
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 [EMAIL PROTECTED] 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
~~ - ([EMAIL PROTECTED])  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