113

I want to extract the first (or last) n characters of a string. This would be the equivalent to Excel's LEFT() and RIGHT(). A small example:

# create a string
a <- paste('left', 'right', sep = '')
a
# [1] "leftright"

I would like to produce b, a string which is equal to the first 4 letters of a:

b
# [1] "left"

What should I do?

Henrik
  • 65,555
  • 14
  • 143
  • 159
Lisa Ann
  • 3,345
  • 6
  • 31
  • 42

5 Answers5

114

See ?substr

R> substr(a, 1, 4)
[1] "left"
rcs
  • 67,191
  • 22
  • 172
  • 153
  • I recommend having a look at @juba's answer below. He provides a `stringr` solution. – Jochem Aug 27 '20 at 15:04
  • 1
    Yeah, substr is a great answer if you only need to read from left to right, but useless if you need to read right to left. For example, if you don't know the number of characters in a string, just that you need to capture the last 10 characters, you cannot use substr; however, with str_sub("??1234567890", -10, -1) = "1234567890" – Steven Mar 25 '23 at 12:18
84

The stringr package provides the str_sub function, which is a bit easier to use than substr, especially if you want to extract right portions of your string :

R> str_sub("leftright",1,4)
[1] "left"
R> str_sub("leftright",-5,-1)
[1] "right"
juba
  • 47,631
  • 14
  • 113
  • 118
  • 3
    Thank for your hint, juba; by the way I think I will accept rcs' answer because it deals with base `R` :) – Lisa Ann Apr 09 '13 at 09:01
  • 4
    Knowing base R is good, but if it comes to string functions, your life will be easier if you only use the stringr as mentioned by @juba – Dieter Menne Apr 09 '13 at 09:16
  • 1
    I agree with Dieter. Learning `stringr` will save you almost as much aggravation as `lubridate`. – Andrew Brēza Jun 28 '17 at 01:07
31

You can easily obtain Right() and Left() functions starting from the Rbase package:

  • right function

    right = function (string, char) {
        substr(string,nchar(string)-(char-1),nchar(string))
    }
    
  • left function

    left = function (string,char) {
        substr(string,1,char)
    }
    

you can use those two custom-functions exactly as left() and right() in excel. Hope you will find it useful

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Andrea Cirillo
  • 564
  • 6
  • 9
8

Make it simple and use R basic functions:

# To get the LEFT part:
> substr(a, 1, 4)
[1] "left"
> 
# To get the MIDDLE part:
> substr(a, 3, 7)
[1] "ftrig"
> 
# To get the RIGHT part:
> substr(a, 5, 10)
[1] "right"

The substr() function tells you where start and stop substr(x, start, stop)

Marcos RF
  • 327
  • 2
  • 8
7

For those coming from Microsoft Excel or Google Sheets, you would have seen functions like LEFT(), RIGHT(), and MID(). I have created a package known as forstringr and its development version is currently on Github.

if(!require("devtools")){
 install.packages("devtools")
}

devtools::install_github("gbganalyst/forstringr")

library(forstringr)
  • the str_left(): This counts from the left and then extract n characters

  • the str_right()- This counts from the right and then extract n characters

  • the str_mid()- This extract characters from the middle

Examples:


x <- "some text in a string"

str_left(x, 4)

[1] "some"

str_right(x, 6)

[1] "string"

str_mid(x, 6, 4)

[1] "text"

gbganalyst
  • 364
  • 4
  • 8