从字符串中提取字符

时间:2013-02-09 16:40:07

标签: r

如何将所有字符提取到指定字符?举个例子,我想在“。”之前提取所有内容。 (周期):

a<-c("asdasd.sss","segssddfge.sss","se.sss")

我想回来:

asdasd segssddfge se

我试过了:

substr(a,1,".")

但它似乎不起作用。

任何想法?

3 个答案:

答案 0 :(得分:7)

这是一个非常基本的方法:

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

答案 1 :(得分:4)

使用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)

使用subtrgregexpr(假设只有1 .并且向量中的所有字符串都有明确匹配。)

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

答案 2 :(得分:2)

尝试使用gsub

gsub(pattern='(.*)[.](.*)','\\1', c("asdasd.sss","segssddfge.sss","se.sss"))
[1] "asdasd"     "segssddfge" "se"