I'm just beginning R, with book Using R for Introductory Statistics, and one of the early questions has me baffled. The question is, create the sequence: 1,2,3,4,5,4,3,2,1 using seq() and rep(). Now, as a programmer, I am punching myself to not be able to figure it out.. I mean, as simple as a for loop, but using seq, I am stumped. I would think c(1:5, 4:1) would be the brute force method with very non intelligent coding.. there has to be a way to make the "turning point" (in this case 5) parametric right? So you could change it later and the sequence will reflect it. -- View this message in context: http://n4.nabble.com/how-to-make-this-sequence-1-2-3-4-5-4-3-2-1-tp1579245p1579245.html Sent from the R help mailing list archive at Nabble.com.
c(x <- 1:5, rev(x[-length(x)])) On 5 March 2010 07:04, kensuguro <magronbass at gmail.com> wrote:> > I'm just beginning R, with book Using R for Introductory Statistics, and one > of the early questions has me baffled. ?The question is, create the > sequence: 1,2,3,4,5,4,3,2,1 using seq() and rep(). > > Now, as a programmer, I am punching myself to not be able to figure it out.. > I mean, as simple as a for loop, but using seq, I am stumped. ?I would think > c(1:5, 4:1) would be the brute force method with very non intelligent > coding.. ?there has to be a way to make the "turning point" (in this case 5) > parametric right? ?So you could change it later and the sequence will > reflect it. > -- > View this message in context: http://n4.nabble.com/how-to-make-this-sequence-1-2-3-4-5-4-3-2-1-tp1579245p1579245.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. >-- ____________________ Baptiste Augui? Departamento de Qu?mica F?sica, Universidade de Vigo, Campus Universitario, 36310, Vigo, Spain tel: +34 9868 18617 http://webs.uvigo.es/coloides
On 05-Mar-10 06:04:51, kensuguro wrote:> > I'm just beginning R, with book Using R for Introductory Statistics, > and one of the early questions has me baffled. The question is, > create the sequence: 1,2,3,4,5,4,3,2,1 using seq() and rep(). > > Now, as a programmer, I am punching myself to not be able to figure > it out.. > I mean, as simple as a for loop, but using seq, I am stumped. > I would think c(1:5, 4:1) would be the brute force method with very > non intelligent coding.. there has to be a way to make the "turning > point" (in this case 5) parametric right? So you could change it > later and the sequence will reflect it. > -- > View this message in context: > http://n4.nabble.com/how-to-make-this-sequence-1-2-3-4-5-4-3-2-1-tp15792 > 45p1579245.html > Sent from the R help mailing list archive at Nabble.com.You can indeed do it using seq(), and with rep() plus a little help from something else: seq(): c(seq(1,5,1),seq(4,1,-1)) rep(): cumsum(c(rep(1,5),rep(-1,4))) Parametrised "1,2,...,n,(n-1),...,2,1": updown1 <- function(n){ c(seq(1,n,1),seq((n-1),1,-1)) } updown2 <- function(n){ cumsum(c(rep(1,n),rep(-1,(n-1)))) } updown1(10) # [1] 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 updown2(10) # [1] 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 05-Mar-10 Time: 08:14:53 ------------------------------ XFMail ------------------------------
Dimitris Rizopoulos
2010-Mar-05 08:19 UTC
[R] how to make this sequence: 1,2,3,4,5,4,3,2,1
try this: 5 - abs(-4:4) I hope it helps. Best, Dimitris On 3/5/2010 7:04 AM, kensuguro wrote:> > I'm just beginning R, with book Using R for Introductory Statistics, and one > of the early questions has me baffled. The question is, create the > sequence: 1,2,3,4,5,4,3,2,1 using seq() and rep(). > > Now, as a programmer, I am punching myself to not be able to figure it out.. > I mean, as simple as a for loop, but using seq, I am stumped. I would think > c(1:5, 4:1) would be the brute force method with very non intelligent > coding.. there has to be a way to make the "turning point" (in this case 5) > parametric right? So you could change it later and the sequence will > reflect it.-- 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