From: jim holtman <jholtman_at_gmail.com>

Date: Tue, 27 May 2008 20:49:19 -0400

Date: Tue, 27 May 2008 20:49:19 -0400

> 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.0000000 -1.0986123 -0.6931472 0.0000000 -1.3862944
-1.0986123 -0.6931472 0.0000000

[10] -1.6094379 -1.3862944 -1.0986123 -0.6931472 0.0000000

*>
**>
*

On Tue, May 27, 2008 at 8:04 PM, T.D.Rudolph <prairie.picker_at_gmail.com> 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.0000000 -1.0986123 -1.0986123 0.0000000 -1.3862944
**> > -1.3862944 -1.3862944 0.0000000 -1.6094379
**> > [11] -1.6094379 -1.6094379 -1.6094379 0.0000000
**> >>
**> >
**> > 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_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<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:cberry_at_tajo.ucsd.edu UC San Diego
**> > http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego
**> 92093-0901
**> >
**> > ______________________________________________
**> > 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<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_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<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_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.Received on Wed 28 May 2008 - 03:10:20 GMT

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.2.0, at Fri 30 May 2008 - 00:30:41 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*