Re: [R] keep only the first value of a numeric sequence

2014-12-15 Thread jeff6868
Great! Both ways works well for my whole data! Thanks guys! -- View this message in context: http://r.789695.n4.nabble.com/keep-only-the-first-value-of-a-numeric-sequence-tp4700774p4700783.html Sent from the R help mailing list archive at Nabble.com. _

Re: [R] keep only the first value of a numeric sequence

2014-12-15 Thread Richard M. Heiberger
we can speed this up a lot > system.time(for (i in 1:2000) ifelse(c(0, diff(data$mydata)) != 1, 0, 1)) user system elapsed 0.122 0.012 0.132 > system.time(for (i in 1:2000) as.numeric( c(0, diff(data$mydata))==1)) user system elapsed 0.073 0.007 0.078 > ifelse(c(0, diff(data$my

Re: [R] keep only the first value of a numeric sequence

2014-12-15 Thread Jorge I Velez
Dear jeff6868, Here is one way: ifelse(with(data, c(0, diff(mydata))) != 1, 0, 1) You could also take a look at ?rle HTH, Jorge.- On Mon, Dec 15, 2014 at 9:33 PM, jeff6868 wrote: > > Hello dear R-helpers, > > I have a small problem in my algorithm. I have sequences of "0" and "1" > values i

[R] keep only the first value of a numeric sequence

2014-12-15 Thread jeff6868
Hello dear R-helpers, I have a small problem in my algorithm. I have sequences of "0" and "1" values in a column of a huge data frame, and I just would like to keep the first value of each sequences of "1" values, such like in this example: data <- data.frame(mydata=c(0,0,0,1,1,1,1,1,0,0,0,0,1,1,