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

时间:2022-01-30 20:24:43

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:

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

# 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,一个等于a的前4个字母的字符串:

b
# [1] "left"

What should I do?

我该怎么办?

4 个解决方案

#1


49  

See ?substr

见?substr

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

#2


29  

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 :

stringr包提供了str_sub函数,它比substr更容易使用,特别是如果你想提取字符串的正确部分:

R> str_sub("leftright",1,4)
[1] "left"
R> str_sub("leftright",-5,-1)
[1] "right"

#3


15  

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

您可以从Rbase包开始轻松获取Right()和Left()函数:

  • right function

    正确的功能

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

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

  • left function

    左功能

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

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

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

您可以在excel中将这两个自定义函数完全用作left()和right()。希望你会发现它很有用

#4


1  

Make it simple and use R basic functions:

简单使用R基本功能:

# 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)

substr()函数告诉你在哪里开始和停止substr(x,start,stop)

#1


49  

See ?substr

见?substr

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

#2


29  

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 :

stringr包提供了str_sub函数,它比substr更容易使用,特别是如果你想提取字符串的正确部分:

R> str_sub("leftright",1,4)
[1] "left"
R> str_sub("leftright",-5,-1)
[1] "right"

#3


15  

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

您可以从Rbase包开始轻松获取Right()和Left()函数:

  • right function

    正确的功能

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

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

  • left function

    左功能

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

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

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

您可以在excel中将这两个自定义函数完全用作left()和right()。希望你会发现它很有用

#4


1  

Make it simple and use R basic functions:

简单使用R基本功能:

# 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)

substr()函数告诉你在哪里开始和停止substr(x,start,stop)