string 从R中的某个字符截断字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6861740/
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
truncate string from a certain character in R
提问by user802231
I have a list of strings in R which looks like:
我在 R 中有一个字符串列表,它看起来像:
WDN.TO
WDR.N
WDS.AX
WEC.AX
WEC.N
WED.TO
I want to get all the postfix of the strings starting from the character ".", the result should look like:
我想从字符“.”开始获取字符串的所有后缀,结果应该如下所示:
.TO
.N
.AX
.AX
.N
.TO
Anyone have any ideas?
谁有想法?
回答by Tommy
Joshua's solution works fine. I'd use sub
instead of gsub
though. gsub
is for substituting multiple occurrences of a pattern in a string - sub
is for one occurrence. The pattern can be simplified a bit too:
约书亚的解决方案工作正常。我会用sub
而不是gsub
。gsub
用于替换字符串中多次出现的模式 - 用于替换sub
一次。该模式也可以简化一点:
> x <- c("WDN.TO","WDR.N","WDS.AX","WEC.AX","WEC.N","WED.TO")
> sub("^[^.]*", "", x)
[1] ".TO" ".N" ".AX" ".AX" ".N" ".TO"
...But if the strings are as regular as in the question, then simply stripping the first 3 characters should be enough:
...但如果字符串和问题中一样规则,那么简单地去除前 3 个字符就足够了:
> x <- c("WDN.TO","WDR.N","WDS.AX","WEC.AX","WEC.N","WED.TO")
> substring(x, 4)
[1] ".TO" ".N" ".AX" ".AX" ".N" ".TO"
回答by Joshua Ulrich
Using gsub
:
使用gsub
:
x <- c("WDN.TO","WDS.N")
# replace everything from the start of the string to the "." with "."
gsub("^.*\.",".",x)
# [1] ".TO" ".N"
Using strsplit
:
使用strsplit
:
# strsplit returns a list; use sapply to get the 2nd obs of each list element
y <- sapply(strsplit(x,"\."), `[`, 2)
# since we split on ".", we need to put it back
paste(".",y,sep="")
# [1] ".TO" ".N"
回答by Aayush Agrawal
Strsplit might do it but in case the data set is too large it will show an error subscript out of bounds
Strsplit 可能会这样做,但如果数据集太大,它将显示错误下标越界
x <- c("WDN.TO","WDR.N","WDS.AX","WEC.AX","WEC.N","WED.TO")
y <- strsplit(x,".")[,2]
#output y= TO N AX AX N TO