[R] Counting value changes
Petr Savicky
savicky at cs.cas.cz
Sat Feb 18 21:31:13 CET 2012
On Sat, Feb 18, 2012 at 11:51:39AM -0800, Pete Brecknock wrote:
>
> maris478 wrote
> >
> > Good afternoon,
> > I've encountered a little bit of a problem, would appreciate any help
> > here.
> >
> > I made a small vector consisting of ones and zeros.
> > Something like this x <- c(0,1,0,1,0,0,1,0), and all I need is to count
> > how many times "0" becomes "1".
> > Tried various, of what I thought, methods with built in functions. Didn't
> > get any further.
Hi.
Do you mean the number of occurences of subsequence "0,1" ?
Try
x <- c(1,0,1,0,1,0,0,1,0)
sum(diff(x) == 1)
[1] 3
The first 1 is not counted, since it is not preceded by 0.
Hope this helps.
Petr Savicky.
More information about the R-help
mailing list