[R] Omitting repeated occurrence in a string

David Winsemius dwinsemius at comcast.net
Wed Feb 6 20:24:11 CET 2013


On Feb 6, 2013, at 8:46 AM, Christofer Bogaso wrote:

> Hello again,
> 
> I was looking for some way on How to delete repeated appearance in a
> String. Let say I have following string:
> 
> Text <- "ahsgdvasgAbcabcsdahj"
> 
> Here you see "Abc" appears twice. But I want to keep only 1
> occurrence. Therefore I need that:
> 
> Text_result <- "ahsgdvasgAbcsdahj" (i.e. the first one).
> 
> Can somebody help me if it is possible using some R function?

This is not going to solve all possible variations of this problem, but then you proposed testing suite was rather limited, ... don't you agree?

> Text <- "ahsgdvasgAbcabcsdahabcj"
> gsub("(abc).*(abc)", "\\1", Text, ignore.case=TRUE)
[1] "ahsgdvasgAbcj"


-- 
David Winsemius
Alameda, CA, USA



More information about the R-help mailing list