[R] how to make this sequence: 1,2,3,4,5,4,3,2,1

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Fri Mar 5 09:19:10 CET 2010


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



More information about the R-help mailing list