An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130125/4a425212/attachment.pl>
How to extract elements from vector in reverse order?
6 messages · hp wan, Nordlund, Dan (DSHS/RDA), David Winsemius +1 more
-----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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130125/35e2cd20/attachment.pl>
On Jan 24, 2013, at 4:23 PM, hp wan wrote:
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) ?
?tail
In Matlab, It can easily achieve by y=x(end-2:end)
y <- tail(x, 3)
David Winsemius Alameda, CA, USA
Hi, ?x<-1:80 y<- x[-(1:77)] y #[1] 78 79 80 #or ?tail() #already suggested If you want only the last element,, library(pastecs) last(x) #[1] 80 A.K. ----- Original Message ----- From: hp wan <huaping.wan at gmail.com> To: r-help at r-project.org Cc: Sent: Thursday, January 24, 2013 7:23 PM 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 ??? [[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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130125/3f493aab/attachment.pl>