Re: [R] Identifying common prefixes from a vector of words, and delete those prefixes

From: Christos Hatzis <christos.hatzis_at_nuverabio.com>
Date: Thu, 31 Jul 2008 13:16:49 -0400

A more general solution:

strip.fun <- function(x, split=".") {

	xx <- strsplit(x, split, fixed=TRUE)
 	txx <- table(unlist(xx))
	nxx <- names(txx)[txx > 1]
	setdiff(unlist(xx), nxx)

}

> x <- c("dog.is.an.animal", "cat.is.an.animal", "rat.is.an.animal")
> strip.fun(x)
[1] "dog" "cat" "rat"

> y <- c("my_cat_pet", "my_dog_pet", "my_rat_pet")
> strip.fun(y, "_")

[1] "cat" "dog" "rat"

-Christos

> -----Original Message-----
> From: r-help-bounces_at_r-project.org
> [mailto:r-help-bounces_at_r-project.org] On Behalf Of John Kane
> Sent: Thursday, July 31, 2008 12:48 PM
> To: r-help_at_stat.math.ethz.ch; Daren Tan
> Subject: Re: [R] Identifying common prefixes from a vector of
> words,and delete those prefixes
>
> There MUST be a better way but this will work.
>
> x <- c("dog.is.an.animal", "cat.is.an.animal",
> "rat.is.an.animal") bb <- strsplit(x, "\\.") myfun <-
> function(m) m[1] animals <- unlist(lapply(bb, myfun)) animals
>
>
>
>
> --- On Thu, 7/31/08, Daren Tan <daren76_at_hotmail.com> wrote:
>
> > From: Daren Tan <daren76_at_hotmail.com>
> > Subject: [R] Identifying common prefixes from a vector of
> words, and
> > delete those prefixes
> > To: r-help_at_stat.math.ethz.ch
> > Received: Thursday, July 31, 2008, 7:11 AM For example,
> > c("dog.is.an.animal", "cat.is.an.animal", "rat.is.an.animal").
> > How can I identify the common prefix is ".is.an.animal" and
> delete it
> > to give c("dog", "cat", "rat") ?
> >
> > Thanks
> > _________________________________________________________________
> >
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > 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.
>
>
>
> __________________________________________________________________
> [[elided Yahoo spam]]
>
> ______________________________________________
> 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.
>
>



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. Received on Thu 31 Jul 2008 - 17:12:21 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 31 Jul 2008 - 18:33:02 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive