string 从 R 中的字符串中提取最后 n 个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7963898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:15:09  来源:igfitidea点击:

Extracting the last n characters from a string in R

rstringsubstring

提问by Brani

How can I get the last n characters from a string in R? Is there a function like SQL's RIGHT?

如何从R中的字符串中获取最后n个字符?有没有像 SQL 的 RIGHT 这样的函数?

回答by Andrie

I'm not aware of anything in base R, but it's straight-forward to make a function to do this using substrand nchar:

我不知道基本 R 中的任何内容,但是使用substrand制作一个函数来执行此操作很简单nchar

x <- "some text in a string"

substrRight <- function(x, n){
  substr(x, nchar(x)-n+1, nchar(x))
}

substrRight(x, 6)
[1] "string"

substrRight(x, 8)
[1] "a string"


This is vectorised, as @mdsumner points out. Consider:

正如@mdsumner 指出的那样,这是矢量化的。考虑:

x <- c("some text in a string", "I really need to learn how to count")
substrRight(x, 6)
[1] "string" " count"

回答by Xu Wang

If you don't mind using the stringrpackage, str_subis handy because you can use negatives to count backward:

如果你不介意使用这个stringr包,这str_sub很方便,因为你可以使用负数来倒数:

x <- "some text in a string"
str_sub(x,-6,-1)
[1] "string"

Or, as Max points out in a comment to this answer,

或者,正如 Max 在对此答案的评论中指出的那样,

str_sub(x, start= -6)
[1] "string"

回答by bartektartanus

Use stri_subfunction from stringipackage. To get substring from the end, use negative numbers. Look below for the examples:

使用包中的stri_sub函数stringi。要从末尾获取子字符串,请使用负数。看看下面的例子:

stri_sub("abcde",1,3)
[1] "abc"
stri_sub("abcde",1,1)
[1] "a"
stri_sub("abcde",-3,-1)
[1] "cde"

You can install this package from github: https://github.com/Rexamine/stringi

你可以从 github 安装这个包:https: //github.com/Rexamine/stringi

It is available on CRAN now, simply type

它现在可在 CRAN 上使用,只需键入

install.packages("stringi")

to install this package.

安装这个包。

回答by Andrew

str = 'This is an example'
n = 7
result = substr(str,(nchar(str)+1)-n,nchar(str))
print(result)

> [1] "example"
> 

回答by dsb

Another reasonably straightforward way is to use regular expressions and sub:

另一种相当直接的方法是使用正则表达式和sub

sub('.*(?=.$)', '', string, perl=T)

So, "get rid of everything followed by one character". To grab more characters off the end, add however many dots in the lookahead assertion:

因此,“摆脱所有后跟一个字符的内容”。要从末尾获取更多字符,请在先行断言中添加许多点:

sub('.*(?=.{2}$)', '', string, perl=T)

where .{2}means .., or "any two characters", so meaning "get rid of everything followed by two characters".

where.{2}表示..,或“任意两个字符”,意思是“去掉后面跟着两个字符的所有内容”。

sub('.*(?=.{3}$)', '', string, perl=T)

for three characters, etc. You can set the number of characters to grab with a variable, but you'll have to pastethe variable value into the regular expression string:

三个字符等。您可以设置要使用变量抓取的字符数,但您必须将paste变量值放入正则表达式字符串中:

n = 3
sub(paste('.+(?=.{', n, '})', sep=''), '', string, perl=T)

回答by Laurent

UPDATE: as noted by mdsumner, the original code is already vectorised because substr is. Should have been more careful.

更新:正如mdsumner所指出的,原始代码已经矢量化了,因为 substr 是。应该更加小心。

And if you want a vectorised version (based on Andrie's code)

如果你想要一个矢量化版本(基于Andrie的代码)

substrRight <- function(x, n){
  sapply(x, function(xx)
         substr(xx, (nchar(xx)-n+1), nchar(xx))
         )
}

> substrRight(c("12345","ABCDE"),2)
12345 ABCDE
 "45"  "DE"

Note that I have changed (nchar(x)-n)to (nchar(x)-n+1)to get ncharacters.

请注意,我已经改变(nchar(x)-n),以(nchar(x)-n+1)获得n字符。

回答by Andrew Haynes

A simple base R solution using the substring()function (who knew this function even existed?):

使用该substring()函数的简单基本 R 解决方案(谁知道这个函数甚至存在?):

RIGHT = function(x,n){
  substring(x,nchar(x)-n+1)
}

This takes advantage of basically being substr()underneath but has a default end value of 1,000,000.

这利用了基本上substr()低于但具有 1,000,000 的默认结束值。

Examples:

例子:

> RIGHT('Hello World!',2)
[1] "d!"
> RIGHT('Hello World!',8)
[1] "o World!"

回答by mdsumner

An alternative to substris to split the string into a list of single characters and process that:

另一种方法substr是将字符串拆分为单个字符列表并处理:

N <- 2
sapply(strsplit(x, ""), function(x, n) paste(tail(x, n), collapse = ""), N)

回答by Raimi bin Karim

I use substrtoo, but in a different way. I want to extract the last 6 characters of "Give me your food." Here are the steps:

我也用substr,但方式不同。我想提取“给我你的食物”的最后 6 个字符。以下是步骤:

(1) Split the characters

(1) 拆分字符

splits <- strsplit("Give me your food.", split = "")

(2) Extract the last 6 characters

(2) 提取最后6个字符

tail(splits[[1]], n=6)

Output:

输出:

[1] " " "f" "o" "o" "d" "."

Each of the character can be accessed by splits[[1]][x], where x is 1 to 6.

每个字符都可以通过 访问splits[[1]][x],其中 x 是 1 到 6。

回答by lukasz

Try this:

尝试这个:

x <- "some text in a string"
n <- 5
substr(x, nchar(x)-n, nchar(x))

It shoudl give:

它应该给出:

[1] "string"