如何提取指定字符的所有字符?举例来说,我想提取“。”之前的所有内容。 (期):从字符串中提取字符
a<-c("asdasd.sss","segssddfge.sss","se.sss")
我想回去:
asdasd segssddfge se
我想:
substr(a,1,".")
,但它似乎并没有工作。
有什么想法?
如何提取指定字符的所有字符?举例来说,我想提取“。”之前的所有内容。 (期):从字符串中提取字符
a<-c("asdasd.sss","segssddfge.sss","se.sss")
我想回去:
asdasd segssddfge se
我想:
substr(a,1,".")
,但它似乎并没有工作。
有什么想法?
这里是一个非常基本的方法:
sapply(strsplit(a, "\\."), `[[`, 1)
# [1] "asdasd" "segssddfge" "se"
而另:
sub(".sss", "", a, fixed = TRUE)
# [1] "asdasd" "segssddfge" "se"
## OR sub("(.*)\\..*", "\\1", a)
## And possibly other variations
@Arun,忘了加上“'固定= TRUE;”这是我的方法,企图正在基于对OP数据的假设(也许是错误的)。谢谢。 – A5C1D2H2I1M1N2O1R2T1 2013-02-09 17:00:32
使用sub
:
# match a "." (escape with "\" to search for "." as a normal "."
# means "any character") followed by 0 to any amount of characters
# until the end of the string and replace with nothing ("")
sub("\\..*$", "", a)
使用subtr
和gregexpr
(假设只有1 .
有,有一个在所有条纹确定匹配向量中的ngs)。
# get the match position of a "." for every string in "a" (returns a list)
# unlist it and get the substring of each from 1 to match.position - 1
substr(a, 1, unlist(gregexpr("\\.", a)) - 1)
这里使用gsub
gsub(pattern='(.*)[.](.*)','\\1', c("asdasd.sss","segssddfge.sss","se.sss"))
[1] "asdasd" "segssddfge" "se"
它是一个CSV文件,所以应该只有一个“” – user1234440 2013-02-09 16:44:07