[R] NOT-SO-SIMPLE function!
T.D.Rudolph
prairie.picker at gmail.com
Tue Jun 3 06:49:58 CEST 2008
I am continually amazed at how many ways there are to skin a cat in R!
Gabor Grothendieck wrote:
>
> In the following cs increases by 1 for each nonzero
> entry in x. Thus the zeros plus the preceding nonzero
> form runs in cs. seq(cs)-match(cs,cs) assigns 0
> to the first element of each such run, 1 to the next,
> 2 to the next and so on. Thus shifting it forward and
> adding 1 gives the number of preceding zeros, num0p1.
>
> cs <- cumsum(x != 0)
> num0p1 <- head(c(0, seq(cs) - match(cs, cs)), -1) + 1
> ifelse(x == 0, NA, log(x/num0p1))
>
> On Mon, Jun 2, 2008 at 2:30 PM, T.D.Rudolph <prairie.picker at gmail.com>
> wrote:
>>
>> 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
>> desired:
>>
>> 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
>> it.
>> I would be indebted to anyone who could with success - it seemed so
>> simple
>> at the offset!
>> Tyler
>> --
>> 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.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
--
View this message in context: http://www.nabble.com/NOT-SO-SIMPLE-function%21-tp17607348p17615995.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list