[R] r programming help II

Mohammad Ehsanul Karim wildscop at yahoo.com
Fri Jun 24 06:19:07 CEST 2005


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, 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986,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]),
sum(rle(K.JUN[(1:7)+30*9+7*0])$lengths[2]),sum(rle(K.JUN[(1:7)+30*9+7*1])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*9+7*2])$lengths[2]),sum(rle(K.JUN[(1:7)+30*9+7*3])$lengths[1]),
sum(rle(K.JUN[(1:7)+30*10+7*0])$lengths[2]),sum(rle(K.JUN[(1:7)+30*10+7*1])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*10+7*2])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*10+7*3])$lengths[c(1,3)]),
sum(rle(K.JUN[(1:7)+30*11+7*0])$lengths[2]),sum(rle(K.JUN[(1:7)+30*11+7*1])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*11+7*2])$lengths[c(2,4)]),sum(rle(K.JUN[(1:7)+30*11+7*3])$lengths[c(1,3)]),
sum(rle(K.JUN[(1:7)+30*12+7*0])$lengths[2]),sum(rle(K.JUN[(1:7)+30*12+7*1])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*12+7*2])$lengths[2]),sum(rle(K.JUN[(1:7)+30*12+7*3])$lengths[1]),
sum(rle(K.JUN[(1:7)+30*13+7*0])$lengths[c(2,4)]),sum(rle(K.JUN[(1:7)+30*13+7*1])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*13+7*2])$lengths[2]),sum(rle(K.JUN[(1:7)+30*13+7*3])$lengths[1]),
sum(rle(K.JUN[(1:7)+30*14+7*0])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*14+7*1])$lengths[c(2,4)]),sum(rle(K.JUN[(1:7)+30*14+7*2])$lengths[c(2,4)]),sum(rle(K.JUN[(1:7)+30*14+7*3])$lengths[c(2,4)]),
sum(rle(K.JUN[(1:7)+30*15+7*0])$lengths[2]),sum(rle(K.JUN[(1:7)+30*15+7*1])$lengths[2]),sum(rle(K.JUN[(1:7)+30*15+7*2])$lengths[2]),sum(rle(K.JUN[(1:7)+30*15+7*3])$lengths[1]),
sum(rle(K.JUN[(1:7)+30*16+7*0])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*16+7*1])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*16+7*2])$lengths[2]),sum(rle(K.JUN[(1:7)+30*16+7*3])$lengths[1]),
sum(rle(K.JUN[(1:7)+30*17+7*0])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*17+7*1])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*17+7*2])$lengths[1]),sum(rle(K.JUN[(1:7)+30*17+7*3])$lengths[c(1,3)]),
sum(rle(K.JUN[(1:7)+30*18+7*0])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*18+7*1])$lengths[c(1,3)]),sum(rle(K.JUN[(1:7)+30*18+7*2])$lengths[1]),sum(rle(K.JUN[(1:7)+30*18+7*3])$lengths[c(1,3)]))
# calculating the observed distribution
# This only considers 4 weeks a month, leaving 2 days
of each June
> table(JUN.w)
JUN.w
 0  1  2  3  4  5  6  7 
 1  6  9 13  8 17 15  7 


Now, i know this is a huge problem and time consuming
for us, but is there any way we can solve this
automatically only by specifying K.JUN data variable
(or its particles K.JUN1984,...,K.JUN2002) by means of
R programming?

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

----------------------------------

Mohammad Ehsanul Karim 

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




More information about the R-help mailing list