Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5959883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:44:51+00:00 2026-05-22T18:44:51+00:00

If, for argument’s sake, I want the last five elements of a 10-length vector

  • 0

If, for argument’s sake, I want the last five elements of a 10-length vector in Python, I can use the - operator in the range index like so:

>>> x = range(10)
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x[-5:]
[5, 6, 7, 8, 9]
>>>

What is the best way to do this in R? Is there a cleaner way than my current technique, which is to use the length() function?

> x <- 0:9
> x
 [1] 0 1 2 3 4 5 6 7 8 9
> x[(length(x) - 4):length(x)]
[1] 5 6 7 8 9
> 

The question is related to time series analysis btw where it is often useful to work only on recent data.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:44:52+00:00Added an answer on May 22, 2026 at 6:44 pm

    see ?tail and ?head for some convenient functions:

    > x <- 1:10
    > tail(x,5)
    [1]  6  7  8  9 10
    

    For the argument’s sake : everything but the last five elements would be :

    > head(x,n=-5)
    [1] 1 2 3 4 5
    

    As @Martin Morgan says in the comments, there are two other possibilities which are faster than the tail solution, in case you have to carry this out a million times on a vector of 100 million values. For readibility, I’d go with tail.

    test                                        elapsed    relative 
    tail(x, 5)                                    38.70     5.724852     
    x[length(x) - (4:0)]                           6.76     1.000000     
    x[seq.int(to = length(x), length.out = 5)]     7.53     1.113905     
    

    benchmarking code :

    require(rbenchmark)
    x <- 1:1e8
    do.call(
      benchmark,
      c(list(
        expression(tail(x,5)),
        expression(x[seq.int(to=length(x), length.out=5)]),
        expression(x[length(x)-(4:0)])
      ),  replications=1e6)
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For the sake of argument, I have an object . I can't modify the
For the sake of argument , lets assume that I can't change my file
I love JQuery-style argument parsing and want to use it in my own function:
How can I learn type of argument at function in Haskell ? In python,
As a function argument I get a vector<double>& vec (an output vector, hence non-const)
Can function argument have hint in cfscript (CF9)? CFML style: <cffunction name=myFunc output=false returntype=void>
For argument's sake lets assume a HTML parser. I've read that it tokenizes everything
How can I get argument names and their values passed to a method as
how i can pass argument to windows 2003 batch file and execute it from
For the sake of argument, consider a UI thread as a thread that has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.