[R] Finding the common portion of strings

Bert Gunter gunter.berton at gene.com
Fri Mar 25 19:07:19 CET 2011


Define: "common string of characters" for:

x <- "abcdghi"
y <- "abcghi"
z <- "ghiabc"
u <-  "abghabc"

-- Bert

On Fri, Mar 25, 2011 at 10:34 AM, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
> Try this:
>
> intersect2 <- function (x, y)
> {
>    y <- as.vector(y)
>    y[match(as.vector(x), y, 0L)]
> }
>
> paste(Reduce(intersect2, strsplit(OBJECTS, NULL)), collapse = '')
>
> On Fri, Mar 25, 2011 at 2:27 PM, Dennis Fisher <fisher at plessthan.com> wrote:
>> Colleagues
>>
>> R: 2.12.2
>> OS X
>>
>> I have a set of text objects in the form (I am showing 3 of what is more likely to be 20 or so):
>>        OBJECTS <- c("abcSOMETHINGCOMMONegf", "xSOMETHINGCOMMONyz", "SOMETHINGCOMMONnme")
>>
>> As you can see, all contain "SOMETHINGCOMMON" and the position varies.  But, I don't know what that "SOMETHINGCOMMON" is.
>>
>> Is there an efficient means to find the common string of characters between all of the objects?
>>
>> Dennis
>>
>> Dennis Fisher MD
>> P < (The "P Less Than" Company)
>> Phone: 1-866-PLessThan (1-866-753-7784)
>> Fax: 1-866-PLessThan (1-866-753-7784)
>> www.PLessThan.com
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Bert Gunter
Genentech Nonclinical Biostatistics
467-7374
http://devo.gene.com/groups/devo/depts/ncb/home.shtml



More information about the R-help mailing list