[R] NOT-SO-SIMPLE function!

T.D.Rudolph prairie.picker at gmail.com
Mon Jun 2 20:30:18 CEST 2008

I am trying to set up a function which processes my data according to the
following rules:

1. if (x[i]==0) NA
2. if (x[i]>0) log(x[i]/(number of consecutive zeros immediately preceding
it +1)) 

The data this will apply to include a variety of whole numbers not limited
to 1 & 0, a number of which may appear consecutively and not separated by
zeros.  Below is an example with a detailed explanation of the output

x <- c(3,2,0,1,0,2,0,0,1,0,0,0,0,4,1) 
output desired = c(1.098, 0.69, NA, -0.69, NA, -0.41, NA, NA, 1.098, NA, NA,
NA, NA, -0.22, 0) 

the 1st element, 3, becomes log(3) = 1.098612 
the 2nd element, 2, becomes log(2) = 0.6931472 
the 3rd element, 0, becomes NA (cannot log zero). 
the 4rd element, 1, becomes log(1/(1(number of consecutive zeros immediately
preceding it) + 1 (constant))) = log(1/2) =  -0.6931472 
the 5th element, 0, becomes NA 
the 6th element, 2, becomes log(2/(1(number of consecutive zeros immediately
preceding it) + 1 (constant))) = log(2/3) = -0.4054651 
the 7th and 8th elements, both zeros, become NA 
the 9th element, 1, becomes log(1/(2(number of consecutive zeros immediately
preceding it) + 1 (constant))) = log(1/3) =  1.098612 
the 10-13th elements, all zeros, each become NA 
the 14th element, 4, becomes log(4/(4(number of consecutive zeros
immediately preceding it) + 1 (constant))) = log(4/5) = -0.2231436 
the 15th element, 1, becomes log(1) = 0 

This one has been in the works for some time and I can't quite seem to crack
I would be indebted to anyone who could with success - it seemed so simple
at the offset!
View this message in context: http://www.nabble.com/NOT-SO-SIMPLE-function%21-tp17607348p17607348.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list