

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.


c(x < 1:5, rev(x[length(x)]))
On 5 March 2010 07:04, kensuguro < [hidden email]> 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/howtomakethissequence123454321tp1579245p1579245.html> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, 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______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


On 05Mar10 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/howtomakethissequence123454321tp15792> 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,(n1),...,2,1":
updown1 < function(n){ c(seq(1,n,1),seq((n1),1,1)) }
updown2 < function(n){ cumsum(c(rep(1,n),rep(1,(n1)))) }
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.

EMail: (Ted Harding) < [hidden email]>
Faxtoemail: +44 (0)870 094 0861
Date: 05Mar10 Time: 08:14:53
 XFMail 
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


so basically, it's impossible to do with just seq() and rep().. Doesn't seem like a good question for chapter 1...
Also, problem 1.13 is even more crazy.. it asks you to build the fibonacci sequence. Now I'm a programmer, and so went way ahead in the book to see how functions were written, and just wrote my own function, but again, in chapter 1? (only covered c(), seq(), and rep() at this point) What away kick start a book. Is "Using R project for Introductory Statistics" known to jump around or have out of sequence practice problems? Otherwise it seems to be written very well.


kensuguro <magronbass <at> gmail.com> writes:
>
>
> so basically, it's impossible to do with just seq() and rep().. Doesn't seem
> like a good question for chapter 1...
>
> Also, problem 1.13 is even more crazy.. it asks you to build the fibonacci
> sequence. Now I'm a programmer, and so went way ahead in the book to see
> how functions were written, and just wrote my own function, but again, in
> chapter 1? (only covered c(), seq(), and rep() at this point) What away
> kick start a book. Is "Using R project for Introductory Statistics" known
> to jump around or have out of sequence practice problems? Otherwise it
> seems to be written very well.
Sorry about that one. On the errata page I have:
page 16, exercise 1.12 #5
This one is most easily done using c() and the sequence operator :. (Please
ignore request to use just seq and rep.)
As for 1.13, I just wanted someone to use c() for that one. I don't even define
the Fibonacci sequence, I only wanted some context to a sequence of numbers that
is not an arithmetic progression.
Not sure if the rest of the book "jumps around", but if it seems to to you, I'm
very open to comments about improvements. Just email.
John
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


c(x,(x<1:5)[4:1])
Original Message
From: baptiste auguie [mailto: [hidden email]]
Sent: Friday, March 05, 2010 1:08 AM
To: kensuguro
Cc: [hidden email]
Subject: Re: [R] how to make this sequence: 1,2,3,4,5,4,3,2,1
c(x < 1:5, rev(x[length(x)]))
On 5 March 2010 07:04, kensuguro < [hidden email]> 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..

____________________
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______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


On 03/06/2010 09:35 AM, j verzani wrote:
> ...
> Sorry about that one. On the errata page I have:
>
> page 16, exercise 1.12 #5
> This one is most easily done using c() and the sequence operator :. (Please
> ignore request to use just seq and rep.)
>
And all over the world, perhaps even on other planets where anonymous R
helpers lurk, the readers of the list can cease their trichotillomania
and get a good night's sleep.
Jim
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


On Sun, Mar 7, 2010 at 4:28 AM, Jim Lemon < [hidden email]> wrote:
> On 03/06/2010 09:35 AM, j verzani wrote:
>>
>> ...
>> Sorry about that one. On the errata page I have:
>>
>> page 16, exercise 1.12 #5
>> This one is most easily done using c() and the sequence operator :.
>> (Please
>> ignore request to use just seq and rep.)
>>
> And all over the world, perhaps even on other planets where anonymous R
> helpers lurk, the readers of the list can cease their trichotillomania and
> get a good night's sleep.
>
Thank goodness. Anyways freed from that constraint here is another solution:
pmin(1:9, 9:1)
This does not use c or rep but does use seq and pmin and has a certain
pleasing symmetry to it.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.

