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 Any suggestion?? Thanks!! Cheers, Anisah --------------------------------- [[alternative HTML version deleted]]
rev(1:10) On 12/02/2008, mohamed nur anisah <nuranisah_mohamed at yahoo.com> 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 > > Any suggestion?? Thanks!! > Cheers, > Anisah > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
On 2/12/2008 7:47 AM, 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 > > Any suggestion?? Thanks!! > Cheers, > Anisah?rev rev(1:10) [1] 10 9 8 7 6 5 4 3 2 1> --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
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 %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%