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.
Counting value changes
8 messages · maris478, Pete Brecknock, Petr Savicky +4 more
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.
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.
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".
Since x consists solely of 0's and 1's this is same as sum(diff(x)==1) # 3 in your example Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Pete Brecknock Sent: Saturday, February 18, 2012 11:52 AM To: r-help at r-project.org Subject: Re: [R] Counting value changes 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.
______________________________________________ R-help at 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.
try this:
x <- c(0,1,0,1,0,0,0,0) sum(diff(x) == 1)
[1] 2
On Sat, Feb 18, 2012 at 2:51 PM, Pete Brecknock <Peter.Brecknock at bp.com> 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. 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.
______________________________________________ R-help at 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.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
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
For completeness, if you want to count all possible four transitions:
x <- c(0,1,0,1,0,0,0,1,1,1,0,0,0,1) # lets keep count of the 4 different transitions that can happen indx <- cbind(head(x, -1), tail(x, -1)) %*% c(2, 1) table(indx) # 0=0-0, 1=0-1, 2=1-0, 3=1-1
indx 0 1 2 3 4 4 3 2
On Sat, Feb 18, 2012 at 7:33 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote:
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
______________________________________________ R-help at 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.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
On Feb 19, 2012, at 04:25 , jim holtman wrote:
For completeness, if you want to count all possible four transitions:
x <- c(0,1,0,1,0,0,0,1,1,1,0,0,0,1) # lets keep count of the 4 different transitions that can happen indx <- cbind(head(x, -1), tail(x, -1)) %*% c(2, 1) table(indx) # 0=0-0, 1=0-1, 2=1-0, 3=1-1
indx 0 1 2 3 4 4 3 2
Um, why not just this?
table(from=head(x, -1), to=tail(x, -1))
to from 0 1 0 4 4 1 3 2
Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com