提取字符串的第一个(或最后一个) n 个字符

我想提取字符串的第一个(或最后一个) N字符。这相当于 Excel 的 LEFT()RIGHT()。举个小例子:

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

我想生成一个字符串 b,它等于 a的前4个字母:

b
# [1] "left"

我该怎么办?

204301 次浏览

See ?substr

R> substr(a, 1, 4)
[1] "left"

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"

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

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)

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"