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 8410343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:08:58+00:00 2026-06-10T00:08:58+00:00

There are functions in Excel called left , right , and mid , where

  • 0

There are functions in Excel called left, right, and mid, where you can extract part of the entry from a cell. For example, =left(A1, 3), would return the 3 left most characters in cell A1, and =mid(A1, 3, 4) would start with the the third character in cell A1 and give you characters number 3 – 6. Are there similar functions in R or similarly straightforward ways to do this?

As a simplified sample problem I would like to take a vector

sample<-c("TRIBAL","TRISTO", "RHOSTO", "EUGFRI", "BYRRAT")

and create 3 new vectors that contain the first 3 characters in each entry, the middle 2 characters in each entry, and the last 4 characters in each entry.

A slightly more complicated question that Excel doesn’t have a function for (that I know of) would be how to create a new vector with the 1st, 3rd, and 5th characters from each entry.

  • 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-10T00:08:58+00:00Added an answer on June 10, 2026 at 12:08 am

    You are looking for the function substr or its close relative substring:

    The leading characters are straight-forward:

    substr(sample, 1, 3)
    [1] "TRI" "TRI" "RHO" "EUG" "BYR"
    

    So is extracting some characters at a defined position:

    substr(sample, 2, 3)
    [1] "RI" "RI" "HO" "UG" "YR"
    

    To get the trailing characters, you have two options:

    substr(sample, nchar(sample)-3, nchar(sample))
    [1] "IBAL" "ISTO" "OSTO" "GFRI" "RRAT"
    
    substring(sample, nchar(sample)-3)
    [1] "IBAL" "ISTO" "OSTO" "GFRI" "RRAT"
    

    And your final “complicated” question:

    characters <- function(x, pos){
      sapply(x, function(x)
        paste(sapply(pos, function(i)substr(x, i, i)), collapse=""))
    }
    characters(sample, c(1,3,5))
    TRIBAL TRISTO RHOSTO EUGFRI BYRRAT 
     "TIA"  "TIT"  "ROT"  "EGR"  "BRA" 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I access the functions inside a third-party DLL from VBA (Excel)? Also,
currently, I used PHPExcel to import excel file, there is a function $cell->getCoordinate(); I
Are there WinAPI functions that do the same what can do standard Windows utility
What functions are there to customise the graph, for example the width of the
I would like to do something like add a nice-to-Excel-functions Name property to the
There is a .NET opensource library called NPOI that allows you to manipulate Excel
I hate repeating functions, particularly in Excel formulas. Is there any way that I
Excel appears to not use lazy evaluation for AND() and OR() functions. For example:
I want to use min(5,10) , or Math.max(4,7) . Are there functions to this
I know there are functions to like is_single() that will return data about the

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.