On Thu, 29 Oct 2009 10:21:23 -0700 (PDT) Guido van Steen 
<gvst...@yahoo.com> wrote:
> I would like to transform this dataset into information on the 
> lenght of the first spell of unemployment. (If there are multiple 
> spells I would just like to use the first one.) 

If 'dat' is your data frame (or matrix):

l.sum=apply(dat,1,rle)
f.length=function(x) x$lengths[match(4,x$values)]
sapply(l.sum,f.length)

The results is a vector of the lengths of the first run of '4' for each 
row (i.e., the first element corresponds to the first row, the second 
element to the second row, and so on).

I very often find uses for the 'rle' function. It fast, it's fun, and 
it's great that it's part of base R. :-)

-- 
Karl Ove Hufthammer

______________________________________________
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.

Reply via email to