nymphita
2012-Oct-22 04:39 UTC
[R] Assigning values to several consecutives rows in a sequence while leaving some empty
Hello all, I'm trying to group several consecutives rows (and assigning them the same value) while leaving some of the rows empty (when a certain condition is not fulfilled). My data are locations (xy coordinates), the date/time at which they were measured, and the time span between measures. Somehow simplified, they look like this: <http://r.789695.n4.nabble.com/file/n4646956/image1.png> I'd like to assign a value to every sequence of locations that are measured within a time span of 4 hours, and make my data look like this: <http://r.789695.n4.nabble.com/file/n4646956/image2.png> I've tried several algorithms with a loop "for" plus "ifelse" condition like: Sequence <- for (i in 1:max(ID)) { ifelse (Span <= 4, i+1, "NA") } without any luck. I know my attempt is incorrect, but my programming skills are really basic and I haven't found any similar problem in the web. Any ideas would be very appreciated! -- View this message in context: http://r.789695.n4.nabble.com/Assigning-values-to-several-consecutives-rows-in-a-sequence-while-leaving-some-empty-tp4646956.html Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt
2012-Oct-22 08:15 UTC
[R] Assigning values to several consecutives rows in a sequence while leaving some empty
On Monday, October 22, 2012, nymphita wrote:> Hello all, > I'm trying to group several consecutives rows (and assigning them the same > value) while leaving some of the rows empty (when a certain condition is > not > fulfilled). > > My data are locations (xy coordinates), the date/time at which they were > measured, and the time span between measures. Somehow simplified, they look > like this: > > <http://r.789695.n4.nabble.com/file/n4646956/image1.png> > > I'd like to assign a value to every sequence of locations that are measured > within a time span of 4 hours, and make my data look like this: > > <http://r.789695.n4.nabble.com/file/n4646956/image2.png> > > I've tried several algorithms with a loop "for" plus "ifelse" condition > like: > > Sequence <- for (i in 1:max(ID)) { > ifelse (Span <= 4, i+1, "NA") > }Very rarely is the output of a `for` loop (used that way) useful. I'd recommend you take 15 minutes to reread the sections on vectorization and control structures in the introductory manual which ships with R. To get at it, type help.start() at the prompt. I _think_ you want something like the following in this case, but the question isn't entirely clear to me. Cheers, Michael sequence <- seq_along(Span) is.na(sequence) <- Span > 5> >> without any luck. I know my attempt is incorrect, but my programming skills > are really basic and I haven't found any similar problem in the web. > > Any ideas would be very appreciated! > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Assigning-values-to-several-consecutives-rows-in-a-sequence-while-leaving-some-empty-tp4646956.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org <javascript:;> mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html >seq>>>>> and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Berend Hasselman
2012-Oct-22 08:45 UTC
[R] Assigning values to several consecutives rows in a sequence while leaving some empty
On 22-10-2012, at 06:39, nymphita wrote:> Hello all, > I'm trying to group several consecutives rows (and assigning them the same > value) while leaving some of the rows empty (when a certain condition is not > fulfilled). > > My data are locations (xy coordinates), the date/time at which they were > measured, and the time span between measures. Somehow simplified, they look > like this: > > <http://r.789695.n4.nabble.com/file/n4646956/image1.png> > > I'd like to assign a value to every sequence of locations that are measured > within a time span of 4 hours, and make my data look like this: > > <http://r.789695.n4.nabble.com/file/n4646956/image2.png> >It's better if you insert the data in the mail using dput(). Nobody wants to enter data from an image. Generate your data (partially) Span <- c(NA,4,8,4,4,8,4,8,16,4,4,12) sqnc <- ifelse(Span<=4,1,0) Span sqnc # step by step y1 <- diff(sqnc) y2 <- cumsum(c(1,pmax(y1[-1],0))) # print y1 y2 # And this seems to give your desired result Sequence <- ifelse(sqnc==1,c(0,y2),NA) Sequence You could substitute y1 in the expression for y2 and then substitute in the Sequence expression to make the computation more compact. Berend> I've tried several algorithms with a loop "for" plus "ifelse" condition > like: > > Sequence <- for (i in 1:max(ID)) { > ifelse (Span <= 4, i+1, "NA") > } > > without any luck. I know my attempt is incorrect, but my programming skills > are really basic and I haven't found any similar problem in the web. > > Any ideas would be very appreciated! > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Assigning-values-to-several-consecutives-rows-in-a-sequence-while-leaving-some-empty-tp4646956.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.