[R] How to turn individual consecutive information into survival objects?
Karl Ove Hufthammer
karl at huftis.org
Thu Oct 29 18:36:52 CET 2009
On Thu, 29 Oct 2009 10:21:23 -0700 (PDT) Guido van Steen
<gvsteen at 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
More information about the R-help
mailing list