Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Character Extraction from String

Tags:

r

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?

like image 211
user1234440 Avatar asked Feb 09 '13 16:02

user1234440


3 Answers

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
like image 107
A5C1D2H2I1M1N2O1R2T1 Avatar answered Nov 19 '22 09:11

A5C1D2H2I1M1N2O1R2T1


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)
like image 29
Arun Avatar answered Nov 19 '22 10:11

Arun


Here an attempt using gsub

gsub(pattern='(.*)[.](.*)','\\1', c("asdasd.sss","segssddfge.sss","se.sss"))
[1] "asdasd"     "segssddfge" "se"        
like image 34
agstudy Avatar answered Nov 19 '22 09:11

agstudy