Hi Everyone, quick question before the end of the year. I have soem indices to select data from a bigger sample. I want to select n days before each index and n days after the index. Any clever way to do it. A for loop would do but I wanted to know if there is a moreR-friendly way to approach this Example # InitialIndices i2 = (90, 190, 290) # Indices I want to end up with i3 = c(85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295) # A way to get Final Indices SampleWidth i3 = c(i2) for (j in seq(1, SampleWidth )) { i3 = c(i3, i2 + j ) i3 = c(i3, i2 - j ) } I tried to tackle this with seq and the apply families but got nowhere Thanks and Happy New Year Paolo [[alternative HTML version deleted]]
in my pevious message the line with SampleWidth should be SampleWidth = 5 Paolo On 31 December 2010 18:03, Paolo Rossi <statmailinglists@googlemail.com>wrote:> Hi Everyone, > > quick question before the end of the year. > > I have soem indices to select data from a bigger sample. I want to select n > days before each index and n days after the index. Any clever way to do it. > A for loop would do but I wanted to know if there is a moreR-friendly way to > approach this > > Example > # InitialIndices > i2 = (90, 190, 290) > > # Indices I want to end up with > i3 = c(85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 185, 186, 187, 188, 189, > 190, 191, 192, 193, 194, 195 > 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295) > # A way to get Final Indices > SampleWidth > i3 = c(i2) > for (j in seq(1, SampleWidth )) { > i3 = c(i3, i2 + j ) > i3 = c(i3, i2 - j ) > } > > > I tried to tackle this with seq and the apply families but got nowhere > > Thanks and Happy New Year > > Paolo > > >[[alternative HTML version deleted]]
On 31/12/2010 1:03 PM, Paolo Rossi wrote:> Hi Everyone, > > quick question before the end of the year. > > I have soem indices to select data from a bigger sample. I want to select n > days before each index and n days after the index. Any clever way to do it. > A for loop would do but I wanted to know if there is a moreR-friendly way to > approach thisHow about this: SampleWidth <- 5 samples <- (-SampleWidth):SampleWidth i2 <- c(90, 190, 290) i3 <- as.vector(outer(samples, i2, "+")) Duncan Murdoch> > Example > # InitialIndices > i2 = (90, 190, 290) > > # Indices I want to end up with > i3 = c(85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 185, 186, 187, 188, 189, > 190, 191, 192, 193, 194, 195 > 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295) > # A way to get Final Indices > SampleWidth > i3 = c(i2) > for (j in seq(1, SampleWidth )) { > i3 = c(i3, i2 + j ) > i3 = c(i3, i2 - j ) > } > > > I tried to tackle this with seq and the apply families but got nowhere > > Thanks and Happy New Year > > Paolo > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
On Fri, Dec 31, 2010 at 1:03 PM, Paolo Rossi <statmailinglists at googlemail.com> wrote:> Hi Everyone, > > quick question before the end of the year. > > I have soem indices to select data from a bigger sample. I want to select n > days before each index and n days after the index. Any clever way to do it. > A for loop would do but I wanted to know if there is a moreR-friendly way to > approach this > > Example > # InitialIndices > i2 = (90, 190, 290) > > # Indices I want to end up with > i3 = c(85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 185, 186, 187, 188, 189, > 190, 191, 192, 193, 194, 195 > 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295) > # A way to get Final Indices > SampleWidth > i3 = c(i2) > for (j in seq(1, SampleWidth )) { > i3 = c(i3, i2 + j ) > ?i3 = c(i3, i2 - j ) > ?}Try this: n <- 5 c(sapply(i2, function(x) seq(x - n, x + n))) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
On Fri, 31 Dec 2010, Paolo Rossi wrote:> Hi Everyone, > > quick question before the end of the year. > > I have soem indices to select data from a bigger sample. I want to select n > days before each index and n days after the index. Any clever way to do it.For heavy duty applications involving intervals - many intervals, finding overlapping intervals, set operations, et cetera, you may want to use the IRanges package: http://www.bioconductor.org/help/bioc-views/release/bioc/html/IRanges.html> A for loop would do but I wanted to know if there is a moreR-friendly way to > approach this > > Example > # InitialIndices > i2 = (90, 190, 290) >Like this:> i2 <- c(90, 190, 290) > as.vector( IRanges( start= i2 - 5, end = i2 + 5 ) )[1] 85 86 87 88 89 90 91 92 93 94 95 185 186 187 188 189 190 191 192 [20] 193 194 195 285 286 287 288 289 290 291 292 293 294 295>HTH, Chuck> # Indices I want to end up with > i3 = c(85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 185, 186, 187, 188, 189, > 190, 191, 192, 193, 194, 195 > 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295) > # A way to get Final Indices > SampleWidth > i3 = c(i2) > for (j in seq(1, SampleWidth )) { > i3 = c(i3, i2 + j ) > i3 = c(i3, i2 - j ) > } > > > I tried to tackle this with seq and the apply families but got nowhere > > Thanks and Happy New Year > > Paolo > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >Charles C. Berry Dept of Family/Preventive Medicine cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901