[R] reverse vector elements

Gavin Simpson gavin.simpson at ucl.ac.uk
Tue Feb 12 13:54:53 CET 2008


On Tue, 2008-02-12 at 04:47 -0800, mohamed nur anisah wrote:
> Dear lists,
>    
>   I want to write a function of a vector and reverse the order of its elements. Here is my code:
>    
>   revector<-function(n){
>  y=vector(length=n)
>   for(i in n:1){
>     y[i]=i
>     }
> return(y)
> }
>    
>   i want my output to be like this:
>   y
>    [1] 10  9  8  7  6  5  4  3  2  1

?rev

As in:

> vec <- 1:10
> rev(vec)
 [1] 10  9  8  7  6  5  4  3  2  1

HTH

G

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list