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

  • SEARCH
  • Home
  • 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 9252803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:02:49+00:00 2026-06-18T11:02:49+00:00

I have a vector of strings: v.string <- c(‘abc’, ‘beb’, ‘lol’, ‘heh’, ‘hah’) Is

  • 0

I have a vector of strings:

v.string <- c('abc', 'beb', 'lol', 'heh', 'hah')

Is there a way of extracting the first N elements from the vector? So in the above if I want to extract the first 2, i will get:

'ab','be','lo','he','ha'

Or do I have to do a loop and use substr function? My vector is rather long.

Thanks

  • 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-06-18T11:02:50+00:00Added an answer on June 18, 2026 at 11:02 am

    you are looking for ?substr

     substr( v.string , start = 1 , stop = 2 )
    

    (incorporating @Arun’s comment) if you want to start at the second-to-last letters, you might also use the nchar function, so

    # print the number of characters in each string in your character vector..
    nchar( v.string ) 
    
    # ..which gets used to..
    
    # print the second-to-last character until the end of the string
    substr( v.string , start = nchar( v.string ) - 1 , stop = nchar( v.string ) )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector of strings: x<-c(abc.dat, xyz.dat) First I would like to replace
I have a vector containing pairs of strings: vector<pair<string, string>> list; I want to
I have a vector of strings each containing last and first name of one
I have string and character vector. I would like to find all strings in
I have a Vector < String >. Now i want to store those Strings
I have vector of strings and want to create a fixed with string out
A have a vector of strings in c++: vector<string> myVect = {A, A, A,
I have a vector of strings: std::vector<std::string> fName which holds a list of file
I have a nested vector of strings like: std::vector<std::vector<string>> I want to access eight
I have a vector of strings which are changing its contents for no apparent

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.