From h.wickham at gmail.com  Sat Apr 12 23:23:08 2008
From: h.wickham at gmail.com (hadley wickham)
Date: Sat, 12 Apr 2008 16:23:08 -0500
Subject: [Rd] HOW TO AVOID LOOPS
In-Reply-To: <FC1A822CDEFB4DE38EB135F6BE851F55@your136f2019dc>
References: <FC1A822CDEFB4DE38EB135F6BE851F55@your136f2019dc>
Message-ID: <f8e6ff050804121423w601b622dw525c72aa2c0bf4fe@mail.gmail.com>

On Sat, Apr 12, 2008 at 11:47 AM, carlos martinez
<martinezbula at earthlink.net> wrote:
> > Looking for a simple, effective a minimum execution time solution.
>  >
>  > For a vector as:
>  >
>  > c(0,0,1,0,1,1,1,0,0,1,1,0,1,0,1,1,1,1,1,1)
>  >
>  To transform it to the following vector without using any loops:
>
>  > (0,0,1,0,1,2,3,0,0,1,2,0,1,0,1,2,3,4,5,6)
>  >
>  Appreciate any suggetions.

How about:

unlist(lapply(split(x, cumsum(x == 0)), seq_along)) - 1

Hadley


-- 
http://had.co.nz/



[Rd] HOW TO AVOID LOOPS
hadley wickham