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. Thank you very much. -- View this message in context: http://r.789695.n4.nabble.com/Counting-value-changes-tp4400267p4400267.html Sent from the R help mailing list archive at Nabble.com.
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. > > Thank you very much. >How about ... x <- c(0,1,0,1,0,0,0,0) sum(rle(x)$values) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Counting-value-changes-tp4400267p4400348.html Sent from the R help mailing list archive at Nabble.com.
Just for clarity, I changed your x a bit - in your version, the 0-1 and 1-0 change occurred the same number of times. If all your values are 0 and 1, this will work:> x <- c(0,1,0,1,0,0,0,1,1,1) > table(diff(x))-1 0 1 2 4 3> sum(diff(x) == 1)[1] 3 If other values can occur, it would need some tweaking. Sarah On Sat, Feb 18, 2012 at 2:09 PM, maris478 <maris478 at gmail.com> 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. > > Thank you very much.-- Sarah Goslee http://www.functionaldiversity.org