Re: [R] help with simple function

2013-03-17 Thread Rui Barradas

Hello,

You are missing a '*' in your first try.
(As for the second, in R parenthesis are round, not [])

sqrt(var(Y1) + var(Y2))^2 - 4*(var(Y1)*(var(Y2) - cov(Y1, Y2)^2))

This written as a function becomes

fun <- function(Y1, Y2)
sqrt(var(Y1) + var(Y2))^2 - 4*(var(Y1)*(var(Y2) - cov(Y1, Y2)^2))

x1 <- rnorm(10)
x2 <- rnorm(10)
fun(x1, x2)


Hope this helps,

Rui Barradas

Em 17-03-2013 15:47, Miguel Eduardo Delgado Burbano escreveu:

hello all

I am writing a quite simple script to study dental wear patterns in humans
and I wrote this function

sqrt(var(Y1)+var(Y2))^2-4(var(Y1)*(var(Y2)-cov(Y1,Y2)^2)) but appear this
error message

Error: attempt to apply non-function

alternatively I wrote this
  sqrt(var(Y1)+var(Y2)^2)-4[(var(Y1)*(var(Y2)-cov(Y1,Y2)^2))]

but this error message appear

[1] NA
Warning message:
NAs introduced by coercion

some suggestion to solve this?? thank you so much for your help

Miguel

[[alternative HTML version deleted]]

__
R-help@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@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.


Re: [R] help with simple function

2013-03-17 Thread arun
Hi,

 Y1<- 1:4
 Y2<- 5:8

sqrt(var(Y1)+var(Y2)^2)-4*((var(Y1)*(var(Y2)-cov(Y1,Y2)^2)))
#[1] 9.515593
A.K.

- Original Message -
From: Miguel Eduardo Delgado Burbano 
To: r-help@r-project.org
Cc: 
Sent: Sunday, March 17, 2013 11:47 AM
Subject: [R] help with simple function

hello all

I am writing a quite simple script to study dental wear patterns in humans
and I wrote this function

sqrt(var(Y1)+var(Y2))^2-4(var(Y1)*(var(Y2)-cov(Y1,Y2)^2)) but appear this
error message

Error: attempt to apply non-function

alternatively I wrote this
sqrt(var(Y1)+var(Y2)^2)-4[(var(Y1)*(var(Y2)-cov(Y1,Y2)^2))]

but this error message appear

[1] NA
Warning message:
NAs introduced by coercion

some suggestion to solve this?? thank you so much for your help

Miguel

    [[alternative HTML version deleted]]

__
R-help@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@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.


Re: [R] help with simple function

2013-03-17 Thread Berend Hasselman

On 17-03-2013, at 16:47, Miguel Eduardo Delgado Burbano 
 wrote:

> hello all
> 
> I am writing a quite simple script to study dental wear patterns in humans
> and I wrote this function
> 
> sqrt(var(Y1)+var(Y2))^2-4(var(Y1)*(var(Y2)-cov(Y1,Y2)^2)) but appear this
> error message
> 
> Error: attempt to apply non-function
> 

Write 4*( instead of 4( assuming you want to multiply.

> alternatively I wrote this
> sqrt(var(Y1)+var(Y2)^2)-4[(var(Y1)*(var(Y2)-cov(Y1,Y2)^2))]
> 
> but this error message appear
> 
> [1] NA
> Warning message:
> NAs introduced by coercion
> 

[ is an indexing operator. You are trying to index the number 4. Which is 
impossible.

Berend


> some suggestion to solve this?? thank you so much for your help
> 
> Miguel
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@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@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.


Re: [R] help with simple function

2008-05-29 Thread T.D.Rudolph

Here's an added caveat, with subsequently a more detailed explanation of the
output desired: 
The data this will apply to includes a variety of whole numbers not limited
to 1 & 0, a number of which may appear consecutively and not separated by
zeros! 

e.g. x<-c(3,2,0,1,0,2,0,0,1,0,0,0,0,4,1) 
answer = 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 


T.D.Rudolph wrote:
> 
> I'm trying to build on Jim's approach to change the parameters in the 
> function, with new rules: 
> 
> 1. if (x[i]==0) NA 
> 2. if (x[i]>0) log(x[i]/(number of consecutive zeros immediately preceding
> it +1)) 
> 
> x<-c(1,0,1,0,0,1,0,0,0,1,0,0,0,0,1) 
> # i.e. output desired = c(0, NA, -0.69, NA, NA, -1.098, NA, NA, NA, -1.38, 
> NA, NA, NA, NA, -1.61) 
> y <- rle(x) 
> # attempting to modify Jim's function: 
> result <- lapply(seq_along(y$lengths), function(.indx){ 
>  if (y$values[.indx-1] == 0) 
>  log(y$values[.indx]/seq(y$lengths[.indx-1]+1, by=-1, 
>  length=y$lengths[.indx])) 
>  else rep(log(y$values[.indx]), y$lengths[.indx]) 
> }) 
> # but I am clearly missing something! 
> 
> Does it not work because I haven't addressed what to do with the zeros and 
> log(0)=-Inf? 
> I've tried adding another "ifelse" but I still get the same result. 
> Can someone find the error in my ways?   
> Tyler 
> 
> 
> jholtman wrote:
>> 
>> Does this do what you want:
>> 
>>> x<-c(0,1,0,0,1,0,0,0,1,0,0,0,0,1)
>>> y <- rle(x)
>>> result <- lapply(seq_along(y$lengths), function(.indx){
>> + if (y$values[.indx] == 0)
>> log(y$values[.indx+1]/seq(y$lengths[.indx]+1, by=-1,
>> length=y$lengths[.indx]))
>> + else rep(log(y$values[.indx]), y$lengths[.indx])
>> + })
>>> unlist(result)
>>  [1] -0.6931472  0.000 -1.0986123 -0.6931472  0.000 -1.3862944
>> -1.0986123 -0.6931472  0.000
>> [10] -1.6094379 -1.3862944 -1.0986123 -0.6931472  0.000
>>>
>>>
>> 
>> 
>> On Tue, May 27, 2008 at 8:04 PM, T.D.Rudolph <[EMAIL PROTECTED]>
>> wrote:
>> 
>>>
>>> In fact x[4,2] should = log(x[5,1]/2]
>>> whereas x[3,2] = log(x[5,1/3])
>>>
>>> i.e. The denominator in the log function equals the number of rows
>>> between
>>> m==0 and m>0 (inclusive, hence the "+1")
>>>
>>> Hope this helps!...
>>>
>>>
>>> Charles C. Berry wrote:
>>> >
>>> > On Tue, 27 May 2008, T.D.Rudolph wrote:
>>> >
>>> >>
>>> >>
>>> >> I have a matrix of frequency counts from 0-160.
>>> >> x<-as.matrix(c(0,1,0,0,1,0,0,0,1,0,0,0,0,1))
>>> >>
>>> >> I would like to apply a function creating a new column
>>> (x[,2])containing
>>> >> values equal to:
>>> >> a) log(x[m,1]) if x[m,1] > 0; and
>>> >> b) for all x[m,1]= 0, log(next x[m,1] > 0 / count of preceding zero
>>> >> values
>>> >> +1)
>>> >>
>>> >> for example, x[1,2] should equal log(x[2,1]/2) = log(1/2) =
>>> -0.6931472
>>> >> whereas x[3,2] should equal log(x[5,1]/3) = log (1/3) = -1.098612
>>> >>
>>> >
>>> > If you also intend that x[4,2] == x[3,2] in your example, then this
>>> seems
>>> > what you want:
>>> >
>>> >> rle.x <- rle(x[,1])
>>> >> num <- ifelse(rle.x$values == 0, c(tail(rle.x$values,-1),NA),
>>> >> rle.x$values )
>>> >> denom <- ifelse(rle.x$values == 0 , rle.x$lengths +1 , 1 )
>>> >> rep(log(num/denom),rle.x$lengths)
>>> >   [1] -0.6931472  0.000 -1.0986123 -1.0986123  0.000
>>> -1.3862944
>>> > -1.3862944 -1.3862944  0.000 -1.6094379
>>> > [11] -1.6094379 -1.6094379 -1.6094379  0.000
>>> >>
>>> >
>>> > See
>>> >
>>> >   ?rep
>>> >   ?rle
>>> >   ?tail
>>> >
>>> > HTH,
>>> >
>>> > Chuck
>>> >
>>> >
>>> >> I will be applying this to nrow(x)=~70,000 so I would prefer to not
>>> do
>>> it
>>> >> by
>>> >> hand!
>>> >> Tyler
>>> >>
>>> >>
>>> >>
>>> >> --
>>> >> View this message in context:
>>> >>
>>> http://www.nabble.com/help-with-simple-function-tp17498394p17498394.html
>>> >> Sent from the R help mailing list archive at Nabble.com.
>>> >>
>>> >> __
>>> >> R-help@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

Re: [R] help with simple function

2008-05-29 Thread T.D.Rudolph

Here's an added caveat, with subsequently a more detailed explanation of the
output desired: 
The data this will apply to includes a variety of whole numbers not limited
to 1 & 0, a number of which may appear consecutively and not separated by
zeros!

e.g. x<-c(3,2,0,1,0,2,0,0,1,0,0,0,0,4,1) 
answer = 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
7-8thth elements = 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


T.D.Rudolph wrote:
> 
> I'm trying to build on Jim's approach to change the parameters in the 
> function, with new rules: 
> 
> 1. if (x[i]==0) NA 
> 2. if (x[i]>0) log(x[i]/(number of consecutive zeros immediately preceding
> it +1)) 
> 
> x<-c(1,0,1,0,0,1,0,0,0,1,0,0,0,0,1) 
> # i.e. output desired = c(0, NA, -0.69, NA, NA, -1.098, NA, NA, NA, -1.38, 
> NA, NA, NA, NA, -1.61) 
> y <- rle(x) 
> # attempting to modify Jim's function: 
> result <- lapply(seq_along(y$lengths), function(.indx){ 
>  if (y$values[.indx-1] == 0) 
>  log(y$values[.indx]/seq(y$lengths[.indx-1]+1, by=-1, 
>  length=y$lengths[.indx])) 
>  else rep(log(y$values[.indx]), y$lengths[.indx]) 
> }) 
> # but I am clearly missing something! 
> 
> Does it not work because I haven't addressed what to do with the zeros and 
> log(0)=-Inf? 
> I've tried adding another "ifelse" but I still get the same result. 
> Can someone find the error in my ways?   
> Tyler 
> 
> 
> jholtman wrote:
>> 
>> Does this do what you want:
>> 
>>> x<-c(0,1,0,0,1,0,0,0,1,0,0,0,0,1)
>>> y <- rle(x)
>>> result <- lapply(seq_along(y$lengths), function(.indx){
>> + if (y$values[.indx] == 0)
>> log(y$values[.indx+1]/seq(y$lengths[.indx]+1, by=-1,
>> length=y$lengths[.indx]))
>> + else rep(log(y$values[.indx]), y$lengths[.indx])
>> + })
>>> unlist(result)
>>  [1] -0.6931472  0.000 -1.0986123 -0.6931472  0.000 -1.3862944
>> -1.0986123 -0.6931472  0.000
>> [10] -1.6094379 -1.3862944 -1.0986123 -0.6931472  0.000
>>>
>>>
>> 
>> 
>> On Tue, May 27, 2008 at 8:04 PM, T.D.Rudolph <[EMAIL PROTECTED]>
>> wrote:
>> 
>>>
>>> In fact x[4,2] should = log(x[5,1]/2]
>>> whereas x[3,2] = log(x[5,1/3])
>>>
>>> i.e. The denominator in the log function equals the number of rows
>>> between
>>> m==0 and m>0 (inclusive, hence the "+1")
>>>
>>> Hope this helps!...
>>>
>>>
>>> Charles C. Berry wrote:
>>> >
>>> > On Tue, 27 May 2008, T.D.Rudolph wrote:
>>> >
>>> >>
>>> >>
>>> >> I have a matrix of frequency counts from 0-160.
>>> >> x<-as.matrix(c(0,1,0,0,1,0,0,0,1,0,0,0,0,1))
>>> >>
>>> >> I would like to apply a function creating a new column
>>> (x[,2])containing
>>> >> values equal to:
>>> >> a) log(x[m,1]) if x[m,1] > 0; and
>>> >> b) for all x[m,1]= 0, log(next x[m,1] > 0 / count of preceding zero
>>> >> values
>>> >> +1)
>>> >>
>>> >> for example, x[1,2] should equal log(x[2,1]/2) = log(1/2) =
>>> -0.6931472
>>> >> whereas x[3,2] should equal log(x[5,1]/3) = log (1/3) = -1.098612
>>> >>
>>> >
>>> > If you also intend that x[4,2] == x[3,2] in your example, then this
>>> seems
>>> > what you want:
>>> >
>>> >> rle.x <- rle(x[,1])
>>> >> num <- ifelse(rle.x$values == 0, c(tail(rle.x$values,-1),NA),
>>> >> rle.x$values )
>>> >> denom <- ifelse(rle.x$values == 0 , rle.x$lengths +1 , 1 )
>>> >> rep(log(num/denom),rle.x$lengths)
>>> >   [1] -0.6931472  0.000 -1.0986123 -1.0986123  0.000
>>> -1.3862944
>>> > -1.3862944 -1.3862944  0.000 -1.6094379
>>> > [11] -1.6094379 -1.6094379 -1.6094379  0.000
>>> >>
>>> >
>>> > See
>>> >
>>> >   ?rep
>>> >   ?rle
>>> >   ?tail
>>> >
>>> > HTH,
>>> >
>>> > Chuck
>>> >
>>> >
>>> >> I will be applying this to nrow(x)=~70,000 so I would prefer to not
>>> do
>>> it
>>> >> by
>>> >> hand!
>>> >> Tyler
>>> >>
>>> >>
>>> >>
>>> >> --
>>> >> View this message in context:
>>> >>
>>> http://www.nabble.com/help-with-simple-function-tp17498394p17498394.html
>>> >> Sent from the R help mailing list archive at Nabble.com.
>>> >>
>>> >> __
>>> >> R-help@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 

Re: [R] help with simple function

2008-05-29 Thread T.D.Rudolph

I'm trying to build on Jim's approach to change the parameters in the 
function, with new rules: 

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

x<-c(1,0,1,0,0,1,0,0,0,1,0,0,0,0,1) 
# i.e. output desired = c(0, NA, -0.69, NA, NA, -1.098, NA, NA, NA, -1.38, 
NA, NA, NA, NA, -1.61) 
y <- rle(x) 
# attempting to modify Jim's function: 
result <- lapply(seq_along(y$lengths), function(.indx){ 
 if (y$values[.indx-1] == 0) 
 log(y$values[.indx]/seq(y$lengths[.indx-1]+1, by=-1, 
 length=y$lengths[.indx])) 
 else rep(log(y$values[.indx]), y$lengths[.indx]) 
}) 
# but I am clearly missing something! 

Does it not work because I haven't addressed what to do with the zeros and 
log(0)=-Inf? 
I've tried adding another "ifelse" but I still get the same result. 
Can someone find the error in my ways?   
Tyler 


jholtman wrote:
> 
> Does this do what you want:
> 
>> x<-c(0,1,0,0,1,0,0,0,1,0,0,0,0,1)
>> y <- rle(x)
>> result <- lapply(seq_along(y$lengths), function(.indx){
> + if (y$values[.indx] == 0)
> log(y$values[.indx+1]/seq(y$lengths[.indx]+1, by=-1,
> length=y$lengths[.indx]))
> + else rep(log(y$values[.indx]), y$lengths[.indx])
> + })
>> unlist(result)
>  [1] -0.6931472  0.000 -1.0986123 -0.6931472  0.000 -1.3862944
> -1.0986123 -0.6931472  0.000
> [10] -1.6094379 -1.3862944 -1.0986123 -0.6931472  0.000
>>
>>
> 
> 
> On Tue, May 27, 2008 at 8:04 PM, T.D.Rudolph <[EMAIL PROTECTED]>
> wrote:
> 
>>
>> In fact x[4,2] should = log(x[5,1]/2]
>> whereas x[3,2] = log(x[5,1/3])
>>
>> i.e. The denominator in the log function equals the number of rows
>> between
>> m==0 and m>0 (inclusive, hence the "+1")
>>
>> Hope this helps!...
>>
>>
>> Charles C. Berry wrote:
>> >
>> > On Tue, 27 May 2008, T.D.Rudolph wrote:
>> >
>> >>
>> >>
>> >> I have a matrix of frequency counts from 0-160.
>> >> x<-as.matrix(c(0,1,0,0,1,0,0,0,1,0,0,0,0,1))
>> >>
>> >> I would like to apply a function creating a new column
>> (x[,2])containing
>> >> values equal to:
>> >> a) log(x[m,1]) if x[m,1] > 0; and
>> >> b) for all x[m,1]= 0, log(next x[m,1] > 0 / count of preceding zero
>> >> values
>> >> +1)
>> >>
>> >> for example, x[1,2] should equal log(x[2,1]/2) = log(1/2) = -0.6931472
>> >> whereas x[3,2] should equal log(x[5,1]/3) = log (1/3) = -1.098612
>> >>
>> >
>> > If you also intend that x[4,2] == x[3,2] in your example, then this
>> seems
>> > what you want:
>> >
>> >> rle.x <- rle(x[,1])
>> >> num <- ifelse(rle.x$values == 0, c(tail(rle.x$values,-1),NA),
>> >> rle.x$values )
>> >> denom <- ifelse(rle.x$values == 0 , rle.x$lengths +1 , 1 )
>> >> rep(log(num/denom),rle.x$lengths)
>> >   [1] -0.6931472  0.000 -1.0986123 -1.0986123  0.000 -1.3862944
>> > -1.3862944 -1.3862944  0.000 -1.6094379
>> > [11] -1.6094379 -1.6094379 -1.6094379  0.000
>> >>
>> >
>> > See
>> >
>> >   ?rep
>> >   ?rle
>> >   ?tail
>> >
>> > HTH,
>> >
>> > Chuck
>> >
>> >
>> >> I will be applying this to nrow(x)=~70,000 so I would prefer to not do
>> it
>> >> by
>> >> hand!
>> >> Tyler
>> >>
>> >>
>> >>
>> >> --
>> >> View this message in context:
>> >>
>> http://www.nabble.com/help-with-simple-function-tp17498394p17498394.html
>> >> Sent from the R help mailing list archive at Nabble.com.
>> >>
>> >> __
>> >> R-help@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.
>> >>
>> >
>> > Charles C. Berry(858) 534-2098
>> >  Dept of Family/Preventive
>> > Medicine
>> > E mailto:[EMAIL PROTECTED] UC San Diego
>> > http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego
>> 92093-0901
>> >
>> > __
>> > R-help@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/help-with-simple-function-tp17498394p17502735.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> __
>> R-help@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.
>>
> 
> 
> 
> -- 
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
> 
> What is the problem you are trying to solve?
> 
>   [[alternative HTML vers

Re: [R] help with simple function

2008-05-29 Thread T.D.Rudolph

I'm trying to build on Jim's approach to change the parameters in the 
function, with new rules: 

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

x<-c(1,0,1,0,0,1,0,0,0,1,0,0,0,0,1) 
# i.e. output desired = c(0, NA, -0.69, NA, NA, -1.098, NA, NA, NA, -1.38, 
NA, NA, NA, NA, -1.61) 
y <- rle(x) 
# attempting to modify Jim's function: 
result <- lapply(seq_along(y$lengths), function(.indx){ 
 if (y$values[.indx-1] == 0) 
 log(y$values[.indx]/seq(y$lengths[.indx-1]+1, by=-1, 
 length=y$lengths[.indx])) 
 else rep(log(y$values[.indx]), y$lengths[.indx]) 
}) 
# but I am clearly missing something! 

Does it not work because I haven't addressed what to do with the zeros and 
log(0)=-Inf? 
I've tried adding another "ifelse" but I still get the same result. 
Can someone find the error in my ways?   
Tyler 


T.D.Rudolph wrote:
> 
> 
> I have a matrix of frequency counts from 0-160.
> x<-as.matrix(c(0,1,0,0,1,0,0,0,1,0,0,0,0,1))
> 
> I would like to apply a function creating a new column (x[,2])containing
> values equal to:
> a) log(x[m,1]) if x[m,1] > 0; and
> b) for all x[m,1]= 0, log(next x[m,1] > 0 / count of preceding zero values
> +1)
> 
> for example, x[1,2] should equal log(x[2,1]/2) = log(1/2) = -0.6931472
> whereas x[3,2] should equal log(x[5,1]/3) = log (1/3) = -1.098612
> 
> I will be applying this to nrow(x)=~70,000 so I would prefer to not do it
> by hand!
> Tyler
> 
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/help-with-simple-function-tp17498394p17551527.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] help with simple function

2008-05-29 Thread T.D.Rudolph

I'm trying to build on Jim's approach to change the parameters in the
function, with new rules: 

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

x<-c(1,0,1,0,0,1,0,0,0,1,0,0,0,0,1) 
# i.e. output desired = c(0, NA, -0.69, NA, NA, -1.098, NA, NA, NA, -1.38,
NA, NA, NA, NA, -1.61) 

# attempting to modify Jim's function...: 
y <- rle(x) 
result <- lapply(seq_along(y$lengths), function(.indx){ 
 if (y$values[.indx-1] == 0) 
 log(y$values[.indx]/seq(y$lengths[.indx-1]+1, by=-1, 
 length=y$lengths[.indx])) 
 else rep(log(y$values[.indx]), y$lengths[.indx]) 
}) 
#...I am clearly missing something! 

Does it not work because I haven't addressed what to do with the zeros and
log(0)=-Inf? 
I've tried adding another "ifelse" but I still get the same result. 
Can someone find the error in my ways?   
Tyler 



jholtman wrote:
> 
> Does this do what you want:
> 
>> x<-c(0,1,0,0,1,0,0,0,1,0,0,0,0,1)
>> y <- rle(x)
>> result <- lapply(seq_along(y$lengths), function(.indx){
> + if (y$values[.indx] == 0)
> log(y$values[.indx+1]/seq(y$lengths[.indx]+1, by=-1,
> length=y$lengths[.indx]))
> + else rep(log(y$values[.indx]), y$lengths[.indx])
> + })
>> unlist(result)
>  [1] -0.6931472  0.000 -1.0986123 -0.6931472  0.000 -1.3862944
> -1.0986123 -0.6931472  0.000
> [10] -1.6094379 -1.3862944 -1.0986123 -0.6931472  0.000
>>
>>
> 
> 
> On Tue, May 27, 2008 at 8:04 PM, T.D.Rudolph <[EMAIL PROTECTED]>
> wrote:
> 
>>
>> In fact x[4,2] should = log(x[5,1]/2]
>> whereas x[3,2] = log(x[5,1/3])
>>
>> i.e. The denominator in the log function equals the number of rows
>> between
>> m==0 and m>0 (inclusive, hence the "+1")
>>
>> Hope this helps!...
>>
>>
>> Charles C. Berry wrote:
>> >
>> > On Tue, 27 May 2008, T.D.Rudolph wrote:
>> >
>> >>
>> >>
>> >> I have a matrix of frequency counts from 0-160.
>> >> x<-as.matrix(c(0,1,0,0,1,0,0,0,1,0,0,0,0,1))
>> >>
>> >> I would like to apply a function creating a new column
>> (x[,2])containing
>> >> values equal to:
>> >> a) log(x[m,1]) if x[m,1] > 0; and
>> >> b) for all x[m,1]= 0, log(next x[m,1] > 0 / count of preceding zero
>> >> values
>> >> +1)
>> >>
>> >> for example, x[1,2] should equal log(x[2,1]/2) = log(1/2) = -0.6931472
>> >> whereas x[3,2] should equal log(x[5,1]/3) = log (1/3) = -1.098612
>> >>
>> >
>> > If you also intend that x[4,2] == x[3,2] in your example, then this
>> seems
>> > what you want:
>> >
>> >> rle.x <- rle(x[,1])
>> >> num <- ifelse(rle.x$values == 0, c(tail(rle.x$values,-1),NA),
>> >> rle.x$values )
>> >> denom <- ifelse(rle.x$values == 0 , rle.x$lengths +1 , 1 )
>> >> rep(log(num/denom),rle.x$lengths)
>> >   [1] -0.6931472  0.000 -1.0986123 -1.0986123  0.000 -1.3862944
>> > -1.3862944 -1.3862944  0.000 -1.6094379
>> > [11] -1.6094379 -1.6094379 -1.6094379  0.000
>> >>
>> >
>> > See
>> >
>> >   ?rep
>> >   ?rle
>> >   ?tail
>> >
>> > HTH,
>> >
>> > Chuck
>> >
>> >
>> >> I will be applying this to nrow(x)=~70,000 so I would prefer to not do
>> it
>> >> by
>> >> hand!
>> >> Tyler
>> >>
>> >>
>> >>
>> >> --
>> >> View this message in context:
>> >>
>> http://www.nabble.com/help-with-simple-function-tp17498394p17498394.html
>> >> Sent from the R help mailing list archive at Nabble.com.
>> >>
>> >> __
>> >> R-help@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.
>> >>
>> >
>> > Charles C. Berry(858) 534-2098
>> >  Dept of Family/Preventive
>> > Medicine
>> > E mailto:[EMAIL PROTECTED] UC San Diego
>> > http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego
>> 92093-0901
>> >
>> > __
>> > R-help@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/help-with-simple-function-tp17498394p17502735.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> __
>> R-help@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.
>>
> 
> 
> 
> -- 
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
> 
> What is the problem you are trying to solve?
> 
>   [[alternative HTML vers

Re: [R] help with simple function

2008-05-29 Thread T.D.Rudolph

I'm trying to build on Jim's approach to change the parameters in the
function, with new rules:

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

x<-c(1,0,1,0,0,1,0,0,0,1,0,0,0,0,1)
# i.e. output desired = c(0, NA, -0.69, NA, NA, -1.098, NA, NA, NA, -1.38,
NA, NA, NA, NA, -1.61)
y <- rle(x)
# attempting to modify Jim's function:
result <- lapply(seq_along(y$lengths), function(.indx){
 if (y$values[.indx-1] == 0)
 log(y$values[.indx]/seq(y$lengths[.indx-1]+1, by=-1,
 length=y$lengths[.indx]))
 else rep(log(y$values[.indx]), y$lengths[.indx])
})
# but I am clearly missing something!

Does it not work because I haven't addressed what to do with the zeros and
log(0)=-Inf?
I've tried adding another "ifelse" but I still get the same result.
Can someone find the error in my ways?  
Tyler


jholtman wrote:
> 
> Does this do what you want:
> 
>> x<-c(0,1,0,0,1,0,0,0,1,0,0,0,0,1)
>> y <- rle(x)
>> result <- lapply(seq_along(y$lengths), function(.indx){
> + if (y$values[.indx] == 0)
> log(y$values[.indx+1]/seq(y$lengths[.indx]+1, by=-1,
> length=y$lengths[.indx]))
> + else rep(log(y$values[.indx]), y$lengths[.indx])
> + })
>> unlist(result)
>  [1] -0.6931472  0.000 -1.0986123 -0.6931472  0.000 -1.3862944
> -1.0986123 -0.6931472  0.000
> [10] -1.6094379 -1.3862944 -1.0986123 -0.6931472  0.000
>>
>>
> 
> 
> On Tue, May 27, 2008 at 8:04 PM, T.D.Rudolph <[EMAIL PROTECTED]>
> wrote:
> 
>>
>> In fact x[4,2] should = log(x[5,1]/2]
>> whereas x[3,2] = log(x[5,1/3])
>>
>> i.e. The denominator in the log function equals the number of rows
>> between
>> m==0 and m>0 (inclusive, hence the "+1")
>>
>> Hope this helps!...
>>
>>
>> Charles C. Berry wrote:
>> >
>> > On Tue, 27 May 2008, T.D.Rudolph wrote:
>> >
>> >>
>> >>
>> >> I have a matrix of frequency counts from 0-160.
>> >> x<-as.matrix(c(0,1,0,0,1,0,0,0,1,0,0,0,0,1))
>> >>
>> >> I would like to apply a function creating a new column
>> (x[,2])containing
>> >> values equal to:
>> >> a) log(x[m,1]) if x[m,1] > 0; and
>> >> b) for all x[m,1]= 0, log(next x[m,1] > 0 / count of preceding zero
>> >> values
>> >> +1)
>> >>
>> >> for example, x[1,2] should equal log(x[2,1]/2) = log(1/2) = -0.6931472
>> >> whereas x[3,2] should equal log(x[5,1]/3) = log (1/3) = -1.098612
>> >>
>> >
>> > If you also intend that x[4,2] == x[3,2] in your example, then this
>> seems
>> > what you want:
>> >
>> >> rle.x <- rle(x[,1])
>> >> num <- ifelse(rle.x$values == 0, c(tail(rle.x$values,-1),NA),
>> >> rle.x$values )
>> >> denom <- ifelse(rle.x$values == 0 , rle.x$lengths +1 , 1 )
>> >> rep(log(num/denom),rle.x$lengths)
>> >   [1] -0.6931472  0.000 -1.0986123 -1.0986123  0.000 -1.3862944
>> > -1.3862944 -1.3862944  0.000 -1.6094379
>> > [11] -1.6094379 -1.6094379 -1.6094379  0.000
>> >>
>> >
>> > See
>> >
>> >   ?rep
>> >   ?rle
>> >   ?tail
>> >
>> > HTH,
>> >
>> > Chuck
>> >
>> >
>> >> I will be applying this to nrow(x)=~70,000 so I would prefer to not do
>> it
>> >> by
>> >> hand!
>> >> Tyler
>> >>
>> >>
>> >>
>> >> --
>> >> View this message in context:
>> >>
>> http://www.nabble.com/help-with-simple-function-tp17498394p17498394.html
>> >> Sent from the R help mailing list archive at Nabble.com.
>> >>
>> >> __
>> >> R-help@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.
>> >>
>> >
>> > Charles C. Berry(858) 534-2098
>> >  Dept of Family/Preventive
>> > Medicine
>> > E mailto:[EMAIL PROTECTED] UC San Diego
>> > http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego
>> 92093-0901
>> >
>> > __
>> > R-help@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/help-with-simple-function-tp17498394p17502735.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> __
>> R-help@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.
>>
> 
> 
> 
> -- 
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
> 
> What is the problem you are trying to solve?
> 
>   [[alternative HTML version deleted]]
> 
> __

Re: [R] help with simple function

2008-05-27 Thread T.D.Rudolph

In fact x[4,2] should = log(x[5,1]/2]
whereas x[3,2] = log(x[5,1/3])

i.e. The denominator in the log function equals the number of rows between
m==0 and m>0 (inclusive, hence the "+1")

Hope this helps!


Charles C. Berry wrote:
> 
> On Tue, 27 May 2008, T.D.Rudolph wrote:
> 
>>
>>
>> I have a matrix of frequency counts from 0-160.
>> x<-as.matrix(c(0,1,0,0,1,0,0,0,1,0,0,0,0,1))
>>
>> I would like to apply a function creating a new column (x[,2])containing
>> values equal to:
>> a) log(x[m,1]) if x[m,1] > 0; and
>> b) for all x[m,1]= 0, log(next x[m,1] > 0 / count of preceding zero
>> values
>> +1)
>>
>> for example, x[1,2] should equal log(x[2,1]/2) = log(1/2) = -0.6931472
>> whereas x[3,2] should equal log(x[5,1]/3) = log (1/3) = -1.098612
>>
> 
> If you also intend that x[4,2] == x[3,2] in your example, then this seems 
> what you want:
> 
>> rle.x <- rle(x[,1])
>> num <- ifelse(rle.x$values == 0, c(tail(rle.x$values,-1),NA),
>> rle.x$values )
>> denom <- ifelse(rle.x$values == 0 , rle.x$lengths +1 , 1 )
>> rep(log(num/denom),rle.x$lengths)
>   [1] -0.6931472  0.000 -1.0986123 -1.0986123  0.000 -1.3862944
> -1.3862944 -1.3862944  0.000 -1.6094379
> [11] -1.6094379 -1.6094379 -1.6094379  0.000
>>
> 
> See
> 
>   ?rep
>   ?rle
>   ?tail
> 
> HTH,
> 
> Chuck
> 
> 
>> I will be applying this to nrow(x)=~70,000 so I would prefer to not do it
>> by
>> hand!
>> Tyler
>>
>>
>>
>> -- 
>> View this message in context:
>> http://www.nabble.com/help-with-simple-function-tp17498394p17498394.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> __
>> R-help@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.
>>
> 
> Charles C. Berry(858) 534-2098
>  Dept of Family/Preventive
> Medicine
> E mailto:[EMAIL PROTECTED]UC San Diego
> http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901
> 
> __
> R-help@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/help-with-simple-function-tp17498394p17500891.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] help with simple function

2008-05-27 Thread T.D.Rudolph

Well done Jim.
It always ends up a little more complicated than I originally hoped, but I
certainly couldn't improve on it!


jholtman wrote:
> 
> Does this do what you want:
> 
>> x<-c(0,1,0,0,1,0,0,0,1,0,0,0,0,1)
>> y <- rle(x)
>> result <- lapply(seq_along(y$lengths), function(.indx){
> + if (y$values[.indx] == 0)
> log(y$values[.indx+1]/seq(y$lengths[.indx]+1, by=-1,
> length=y$lengths[.indx]))
> + else rep(log(y$values[.indx]), y$lengths[.indx])
> + })
>> unlist(result)
>  [1] -0.6931472  0.000 -1.0986123 -0.6931472  0.000 -1.3862944
> -1.0986123 -0.6931472  0.000
> [10] -1.6094379 -1.3862944 -1.0986123 -0.6931472  0.000
>>
>>
> 
> 
> On Tue, May 27, 2008 at 8:04 PM, T.D.Rudolph <[EMAIL PROTECTED]>
> wrote:
> 
>>
>> In fact x[4,2] should = log(x[5,1]/2]
>> whereas x[3,2] = log(x[5,1/3])
>>
>> i.e. The denominator in the log function equals the number of rows
>> between
>> m==0 and m>0 (inclusive, hence the "+1")
>>
>> Hope this helps!...
>>
>>
>> Charles C. Berry wrote:
>> >
>> > On Tue, 27 May 2008, T.D.Rudolph wrote:
>> >
>> >>
>> >>
>> >> I have a matrix of frequency counts from 0-160.
>> >> x<-as.matrix(c(0,1,0,0,1,0,0,0,1,0,0,0,0,1))
>> >>
>> >> I would like to apply a function creating a new column
>> (x[,2])containing
>> >> values equal to:
>> >> a) log(x[m,1]) if x[m,1] > 0; and
>> >> b) for all x[m,1]= 0, log(next x[m,1] > 0 / count of preceding zero
>> >> values
>> >> +1)
>> >>
>> >> for example, x[1,2] should equal log(x[2,1]/2) = log(1/2) = -0.6931472
>> >> whereas x[3,2] should equal log(x[5,1]/3) = log (1/3) = -1.098612
>> >>
>> >
>> > If you also intend that x[4,2] == x[3,2] in your example, then this
>> seems
>> > what you want:
>> >
>> >> rle.x <- rle(x[,1])
>> >> num <- ifelse(rle.x$values == 0, c(tail(rle.x$values,-1),NA),
>> >> rle.x$values )
>> >> denom <- ifelse(rle.x$values == 0 , rle.x$lengths +1 , 1 )
>> >> rep(log(num/denom),rle.x$lengths)
>> >   [1] -0.6931472  0.000 -1.0986123 -1.0986123  0.000 -1.3862944
>> > -1.3862944 -1.3862944  0.000 -1.6094379
>> > [11] -1.6094379 -1.6094379 -1.6094379  0.000
>> >>
>> >
>> > See
>> >
>> >   ?rep
>> >   ?rle
>> >   ?tail
>> >
>> > HTH,
>> >
>> > Chuck
>> >
>> >
>> >> I will be applying this to nrow(x)=~70,000 so I would prefer to not do
>> it
>> >> by
>> >> hand!
>> >> Tyler
>> >>
>> >>
>> >>
>> >> --
>> >> View this message in context:
>> >>
>> http://www.nabble.com/help-with-simple-function-tp17498394p17498394.html
>> >> Sent from the R help mailing list archive at Nabble.com.
>> >>
>> >> __
>> >> R-help@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.
>> >>
>> >
>> > Charles C. Berry(858) 534-2098
>> >  Dept of Family/Preventive
>> > Medicine
>> > E mailto:[EMAIL PROTECTED] UC San Diego
>> > http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego
>> 92093-0901
>> >
>> > __
>> > R-help@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/help-with-simple-function-tp17498394p17502735.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> __
>> R-help@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.
>>
> 
> 
> 
> -- 
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
> 
> What is the problem you are trying to solve?
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@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/help-with-simple-function-tp17498394p17503599.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] help with simple function

2008-05-27 Thread jim holtman
Does this do what you want:

> x<-c(0,1,0,0,1,0,0,0,1,0,0,0,0,1)
> y <- rle(x)
> result <- lapply(seq_along(y$lengths), function(.indx){
+ if (y$values[.indx] == 0)
log(y$values[.indx+1]/seq(y$lengths[.indx]+1, by=-1,
length=y$lengths[.indx]))
+ else rep(log(y$values[.indx]), y$lengths[.indx])
+ })
> unlist(result)
 [1] -0.6931472  0.000 -1.0986123 -0.6931472  0.000 -1.3862944
-1.0986123 -0.6931472  0.000
[10] -1.6094379 -1.3862944 -1.0986123 -0.6931472  0.000
>
>


On Tue, May 27, 2008 at 8:04 PM, T.D.Rudolph <[EMAIL PROTECTED]>
wrote:

>
> In fact x[4,2] should = log(x[5,1]/2]
> whereas x[3,2] = log(x[5,1/3])
>
> i.e. The denominator in the log function equals the number of rows between
> m==0 and m>0 (inclusive, hence the "+1")
>
> Hope this helps!...
>
>
> Charles C. Berry wrote:
> >
> > On Tue, 27 May 2008, T.D.Rudolph wrote:
> >
> >>
> >>
> >> I have a matrix of frequency counts from 0-160.
> >> x<-as.matrix(c(0,1,0,0,1,0,0,0,1,0,0,0,0,1))
> >>
> >> I would like to apply a function creating a new column (x[,2])containing
> >> values equal to:
> >> a) log(x[m,1]) if x[m,1] > 0; and
> >> b) for all x[m,1]= 0, log(next x[m,1] > 0 / count of preceding zero
> >> values
> >> +1)
> >>
> >> for example, x[1,2] should equal log(x[2,1]/2) = log(1/2) = -0.6931472
> >> whereas x[3,2] should equal log(x[5,1]/3) = log (1/3) = -1.098612
> >>
> >
> > If you also intend that x[4,2] == x[3,2] in your example, then this seems
> > what you want:
> >
> >> rle.x <- rle(x[,1])
> >> num <- ifelse(rle.x$values == 0, c(tail(rle.x$values,-1),NA),
> >> rle.x$values )
> >> denom <- ifelse(rle.x$values == 0 , rle.x$lengths +1 , 1 )
> >> rep(log(num/denom),rle.x$lengths)
> >   [1] -0.6931472  0.000 -1.0986123 -1.0986123  0.000 -1.3862944
> > -1.3862944 -1.3862944  0.000 -1.6094379
> > [11] -1.6094379 -1.6094379 -1.6094379  0.000
> >>
> >
> > See
> >
> >   ?rep
> >   ?rle
> >   ?tail
> >
> > HTH,
> >
> > Chuck
> >
> >
> >> I will be applying this to nrow(x)=~70,000 so I would prefer to not do
> it
> >> by
> >> hand!
> >> Tyler
> >>
> >>
> >>
> >> --
> >> View this message in context:
> >>
> http://www.nabble.com/help-with-simple-function-tp17498394p17498394.html
> >> Sent from the R help mailing list archive at Nabble.com.
> >>
> >> __
> >> R-help@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.
> >>
> >
> > Charles C. Berry(858) 534-2098
> >  Dept of Family/Preventive
> > Medicine
> > E mailto:[EMAIL PROTECTED] UC San Diego
> > http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego
> 92093-0901
> >
> > __
> > R-help@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/help-with-simple-function-tp17498394p17502735.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] help with simple function

2008-05-27 Thread T.D.Rudolph

In fact x[4,2] should = log(x[5,1]/2] 
whereas x[3,2] = log(x[5,1/3]) 

i.e. The denominator in the log function equals the number of rows between
m==0 and m>0 (inclusive, hence the "+1") 

Hope this helps!... 


Charles C. Berry wrote:
> 
> On Tue, 27 May 2008, T.D.Rudolph wrote:
> 
>>
>>
>> I have a matrix of frequency counts from 0-160.
>> x<-as.matrix(c(0,1,0,0,1,0,0,0,1,0,0,0,0,1))
>>
>> I would like to apply a function creating a new column (x[,2])containing
>> values equal to:
>> a) log(x[m,1]) if x[m,1] > 0; and
>> b) for all x[m,1]= 0, log(next x[m,1] > 0 / count of preceding zero
>> values
>> +1)
>>
>> for example, x[1,2] should equal log(x[2,1]/2) = log(1/2) = -0.6931472
>> whereas x[3,2] should equal log(x[5,1]/3) = log (1/3) = -1.098612
>>
> 
> If you also intend that x[4,2] == x[3,2] in your example, then this seems 
> what you want:
> 
>> rle.x <- rle(x[,1])
>> num <- ifelse(rle.x$values == 0, c(tail(rle.x$values,-1),NA),
>> rle.x$values )
>> denom <- ifelse(rle.x$values == 0 , rle.x$lengths +1 , 1 )
>> rep(log(num/denom),rle.x$lengths)
>   [1] -0.6931472  0.000 -1.0986123 -1.0986123  0.000 -1.3862944
> -1.3862944 -1.3862944  0.000 -1.6094379
> [11] -1.6094379 -1.6094379 -1.6094379  0.000
>>
> 
> See
> 
>   ?rep
>   ?rle
>   ?tail
> 
> HTH,
> 
> Chuck
> 
> 
>> I will be applying this to nrow(x)=~70,000 so I would prefer to not do it
>> by
>> hand!
>> Tyler
>>
>>
>>
>> -- 
>> View this message in context:
>> http://www.nabble.com/help-with-simple-function-tp17498394p17498394.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> __
>> R-help@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.
>>
> 
> Charles C. Berry(858) 534-2098
>  Dept of Family/Preventive
> Medicine
> E mailto:[EMAIL PROTECTED]UC San Diego
> http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901
> 
> __
> R-help@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/help-with-simple-function-tp17498394p17502735.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] help with simple function

2008-05-27 Thread Charles C. Berry

On Tue, 27 May 2008, T.D.Rudolph wrote:




I have a matrix of frequency counts from 0-160.
x<-as.matrix(c(0,1,0,0,1,0,0,0,1,0,0,0,0,1))

I would like to apply a function creating a new column (x[,2])containing
values equal to:
a) log(x[m,1]) if x[m,1] > 0; and
b) for all x[m,1]= 0, log(next x[m,1] > 0 / count of preceding zero values
+1)

for example, x[1,2] should equal log(x[2,1]/2) = log(1/2) = -0.6931472
whereas x[3,2] should equal log(x[5,1]/3) = log (1/3) = -1.098612



If you also intend that x[4,2] == x[3,2] in your example, then this seems 
what you want:



rle.x <- rle(x[,1])
num <- ifelse(rle.x$values == 0, c(tail(rle.x$values,-1),NA), rle.x$values )
denom <- ifelse(rle.x$values == 0 , rle.x$lengths +1 , 1 )
rep(log(num/denom),rle.x$lengths)

 [1] -0.6931472  0.000 -1.0986123 -1.0986123  0.000 -1.3862944 
-1.3862944 -1.3862944  0.000 -1.6094379
[11] -1.6094379 -1.6094379 -1.6094379  0.000




See

?rep
?rle
?tail

HTH,

Chuck



I will be applying this to nrow(x)=~70,000 so I would prefer to not do it by
hand!
Tyler



--
View this message in context: 
http://www.nabble.com/help-with-simple-function-tp17498394p17498394.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.



Charles C. Berry(858) 534-2098
Dept of Family/Preventive Medicine
E mailto:[EMAIL PROTECTED]  UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

__
R-help@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.