[R] How to extract elements from vector in reverse order?

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Fri Jan 25 01:49:13 CET 2013


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of hp wan
> Sent: Thursday, January 24, 2013 4:23 PM
> To: r-help at r-project.org
> Subject: [R] How to extract elements from vector in reverse order?
> 
> Hi all mailing listers,
> 
> I wanna get the last several elments of vector.
>  e.g.  x <- c(1,2,3,.....,78, 79, 80)
> 
> How can I implement to assign last three elements to y,  y <- c(78, 79,
> 80)
> ?
> 
> In Matlab, It can easily achieve by y=x(end-2:end)
> 
> 
> Thanks
> 
> Huaping Wan
> 

You can get what you want in much the same way

y <- x[(length(x)-2):length(x)]


Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204



More information about the R-help mailing list