R 从字符串中提取字符
如何将所有字符提取到指定的字符?例如,我想提取“.”(句点)之前的所有内容: 我试过:R 从字符串中提取字符,r,R,如何将所有字符提取到指定的字符?例如,我想提取“.”(句点)之前的所有内容: 我试过: substr(a,1,".") 但它似乎不起作用 有什么想法吗?这里有一个非常基本的方法: sapply(strsplit(a, "\\."), `[[`, 1) # [1] "asdasd" "segssddfge" "se" 还有一个: sub(".sss", "", a, fixed = TRUE) # [1] "asdasd" "segssddfge" "se" ## OR su
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
使用
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个
,并且所有字符串中都有明确的匹配)
这里尝试使用
gsub
gsub(pattern='(.*)[.](.*)','\\1', c("asdasd.sss","segssddfge.sss","se.sss"))
[1] "asdasd" "segssddfge" "se"
这是一个csv文件,所以应该只有一个“@Arun,忘记添加“
fixed=TRUE
”,这是我基于OP数据的假设(可能是错误的)而采取的方法。谢谢
# 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)
# 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(pattern='(.*)[.](.*)','\\1', c("asdasd.sss","segssddfge.sss","se.sss"))
[1] "asdasd" "segssddfge" "se"