How would you extract all characters up to a specified character? For Example given, I would like to extract everything before the "." (period):
a<-c("asdasd.sss","segssddfge.sss","se.sss")
I would like to get back:
asdasd segssddfge se
I tried:
substr(a,1,".")
but it doesn't seem to work.
any ideas?
Here's a very basic approach:
sapply(strsplit(a, "\\."), `[[`, 1)
# [1] "asdasd" "segssddfge" "se"
And another:
sub(".sss", "", a, fixed = TRUE)
# [1] "asdasd" "segssddfge" "se"
## OR sub("(.*)\\..*", "\\1", a)
## And possibly other variations
Using 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)
Using subtr
and gregexpr
(assuming there's only 1 .
and there's a definite match in all strings within the vector).
# 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)
Here an attempt using gsub
gsub(pattern='(.*)[.](.*)','\\1', c("asdasd.sss","segssddfge.sss","se.sss"))
[1] "asdasd" "segssddfge" "se"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With