Martin Batholdy
2011-Oct-07 13:40 UTC
[R] generate sequences for every element of a vector
Dear R-List, I have the following problem: I have a vector that looks like this: x <- c(3, 8, 14, 24, 34, 40) And I would like to make a sequence out of every element, starting at the value of this element. So that I get a vector just like this: x <- c(3, 4, 5, 8, 9, 10, 14, 15, 16, 24, 25, 26, 34, 35, 36, 40, 41, 42) How can I do this without looping over the whole vector or something like this? thanks for any suggestions!
Jean-Christophe BOUËTTÉ
2011-Oct-07 13:54 UTC
[R] generate sequences for every element of a vector
Hi, this should work: as.vector(rbind(x,x+1,x+2)) 2011/10/7 Martin Batholdy <batholdy at googlemail.com>:> Dear R-List, > > > I have the following problem: > > I have a vector that looks like this: > > x <- c(3, 8, 14, 24, 34, 40) > > > And I would like to make a sequence out of every element, starting at the value of this element. > > So that I get a vector just like this: > > x <- c(3, 4, 5, ? ? 8, 9, 10, ? ? ?14, 15, 16, ? ? ? 24, 25, 26, ? ? ? 34, 35, 36, ? ? ?40, 41, 42) > > > > > How can I do this without looping over the whole vector or something like this? > > > thanks for any suggestions! > ______________________________________________ > 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. >
Martin Batholdy
2011-Oct-07 13:56 UTC
[R] generate sequences for every element of a vector
great, thank you! On 07.10.2011, at 15:54, Jean-Christophe BOU?TT? wrote:> Hi, > this should work: > as.vector(rbind(x,x+1,x+2)) > > 2011/10/7 Martin Batholdy <batholdy at googlemail.com>: >> Dear R-List, >> >> >> I have the following problem: >> >> I have a vector that looks like this: >> >> x <- c(3, 8, 14, 24, 34, 40) >> >> >> And I would like to make a sequence out of every element, starting at the value of this element. >> >> So that I get a vector just like this: >> >> x <- c(3, 4, 5, 8, 9, 10, 14, 15, 16, 24, 25, 26, 34, 35, 36, 40, 41, 42) >> >> >> >> >> How can I do this without looping over the whole vector or something like this? >> >> >> thanks for any suggestions! >> ______________________________________________ >> 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. >>
Jean-Christophe BOUËTTÉ
2011-Oct-07 13:58 UTC
[R] generate sequences for every element of a vector
Or a more general form: n <- 3 as.vector(matrix(rep(x,each=n),nrow=n)+0:(n-1)) 2011/10/7 Jean-Christophe BOU?TT? <jcbouette at gmail.com>:> Hi, > this should work: > as.vector(rbind(x,x+1,x+2)) > > 2011/10/7 Martin Batholdy <batholdy at googlemail.com>: >> Dear R-List, >> >> >> I have the following problem: >> >> I have a vector that looks like this: >> >> x <- c(3, 8, 14, 24, 34, 40) >> >> >> And I would like to make a sequence out of every element, starting at the value of this element. >> >> So that I get a vector just like this: >> >> x <- c(3, 4, 5, ? ? 8, 9, 10, ? ? ?14, 15, 16, ? ? ? 24, 25, 26, ? ? ? 34, 35, 36, ? ? ?40, 41, 42) >> >> >> >> >> How can I do this without looping over the whole vector or something like this? >> >> >> thanks for any suggestions! >> ______________________________________________ >> 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. >> >
Dimitris Rizopoulos
2011-Oct-07 13:58 UTC
[R] generate sequences for every element of a vector
One option is: x <- c(3, 8, 14, 24, 34, 40) c(sapply(x, seq, length = 3)) I hope it helps. Best, Dimitris On 10/7/2011 3:40 PM, Martin Batholdy wrote:> Dear R-List, > > > I have the following problem: > > I have a vector that looks like this: > > x<- c(3, 8, 14, 24, 34, 40) > > > And I would like to make a sequence out of every element, starting at the value of this element. > > So that I get a vector just like this: > > x<- c(3, 4, 5, 8, 9, 10, 14, 15, 16, 24, 25, 26, 34, 35, 36, 40, 41, 42) > > > > > How can I do this without looping over the whole vector or something like this? > > > thanks for any suggestions! > ______________________________________________ > 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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/