[R] Identifying common prefixes from a vector of words, and delete those prefixes
Martin Morgan
mtmorgan at fhcrc.org
Sat Aug 2 04:42:09 CEST 2008
Daren Tan <daren76 at hotmail.com> writes:
> 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") ?
The 'Rlibstree' package from omegahat is quite fun for this sort of
thing:
> install.packages('Rlibstree', repos="http://www.omegahat.org/R")
[snip]
> library(Rlibstree)
> rstrings <- function(string) {
+ lapply(lapply(strsplit(string, ""), rev),
+ paste, collapse="")
+ }
> pets <- c("dog.is.an.animal", "cat.is.an.animal", "rat.is.an.animal")
> commonSfx <- rstrings(getCommonPrefix(rstrings(pets)))
> commonSfx
[[1]]
[1] ".is.an.animal"
> sub(commonSfx, "", pets)
[1] "dog" "cat" "rat"
Martin
> 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.
--
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109
Location: Arnold Building M2 B169
Phone: (206) 667-2793
More information about the R-help
mailing list