Dear R-help list, I would like to replace all lower-case letters in a string that are not part of certain fixed expressions. For example, I have the string: "pmin(pmax(pmin(x1, X2), pmin(X3, X4)) == Y, pmax(Z1, z1))" Where I would like to replace all lower-case letters that do not belong to the functions "pmin" and "pmax" by 1 - toupper(...) to get "pmin(pmax(pmin(1 - X1, X2), pmin(X3, X4)) == Y, pmax(Z1, 1 - Z1))" Any ideas on how I could achieve that? Many thanks and best wishes, Alrik ******************************** Alrik Thiem Post-Doctoral Researcher Department of Philosophy University of Geneva Rue de Candolle 2 CH-1211 Geneva +41 76 527 80 83 http://www.alrik-thiem.net http://www.compasss.org
If your string will always represent an R expression, you could work with the expression directly with functions like all.names() and substitute(). f <- function (expr) { toReplace <- setdiff(all.names(expr), c("pmin", "pmax")) toReplace <- grep(value = TRUE, "[a-z]", toReplace) names(toReplace) <- toReplace replacementList <- lapply(toReplace, function(name) call("-", 1, as.name(toupper(name)))) do.call(substitute, list(expr, replacementList)) }> In <- quote(pmin(pmax(pmin(x1, X2), pmin(X3, X4)) == Y, pmax(Z1, z1))) > Desired <- quote(pmin(pmax(pmin(1 - X1, X2), pmin(X3, X4)) == Y, pmax(Z1,1 - Z1)))> all.equal(Desired, f(In))[1] TRUE Bill Dunlap TIBCO Software wdunlap tibco.com On Fri, Feb 27, 2015 at 2:19 PM, Alrik Thiem <alrik.thiem at gmail.com> wrote:> Dear R-help list, > > I would like to replace all lower-case letters in a string that are not > part > of certain fixed expressions. For example, I have the string: > > "pmin(pmax(pmin(x1, X2), pmin(X3, X4)) == Y, pmax(Z1, z1))" > > Where I would like to replace all lower-case letters that do not belong to > the functions "pmin" and "pmax" by 1 - toupper(...) to get > > "pmin(pmax(pmin(1 - X1, X2), pmin(X3, X4)) == Y, pmax(Z1, 1 - Z1))" > > Any ideas on how I could achieve that? > > Many thanks and best wishes, > > Alrik > > > ******************************** > Alrik Thiem > Post-Doctoral Researcher > > Department of Philosophy > University of Geneva > Rue de Candolle 2 > CH-1211 Geneva > > +41 76 527 80 83 > > http://www.alrik-thiem.net > http://www.compasss.org > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
Many thanks. Unfortunately, I cannot work directly on these expressions since they?re only created from other strings. Would I first have to transform these strings to unevaluated expressions? Von: William Dunlap [mailto:wdunlap at tibco.com] Gesendet: Freitag, 27. Februar 2015 23:39 An: Alrik Thiem Cc: r-help at r-project.org Betreff: Re: [R] Substring replacement in string If your string will always represent an R expression, you could work with the expression directly with functions like all.names() and substitute(). f <- function (expr) { toReplace <- setdiff(all.names(expr), c("pmin", "pmax")) toReplace <- grep(value = TRUE, "[a-z]", toReplace) names(toReplace) <- toReplace replacementList <- lapply(toReplace, function(name) call("-", 1, as.name(toupper(name)))) do.call(substitute, list(expr, replacementList)) }> In <- quote(pmin(pmax(pmin(x1, X2), pmin(X3, X4)) == Y, pmax(Z1, z1))) > Desired <- quote(pmin(pmax(pmin(1 - X1, X2), pmin(X3, X4)) == Y, pmax(Z1, 1 - Z1))) > all.equal(Desired, f(In))[1] TRUE Bill Dunlap TIBCO Software wdunlap tibco.com On Fri, Feb 27, 2015 at 2:19 PM, Alrik Thiem <alrik.thiem at gmail.com> wrote: Dear R-help list, I would like to replace all lower-case letters in a string that are not part of certain fixed expressions. For example, I have the string: "pmin(pmax(pmin(x1, X2), pmin(X3, X4)) == Y, pmax(Z1, z1))" Where I would like to replace all lower-case letters that do not belong to the functions "pmin" and "pmax" by 1 - toupper(...) to get "pmin(pmax(pmin(1 - X1, X2), pmin(X3, X4)) == Y, pmax(Z1, 1 - Z1))" Any ideas on how I could achieve that? Many thanks and best wishes, Alrik ******************************** Alrik Thiem Post-Doctoral Researcher Department of Philosophy University of Geneva Rue de Candolle 2 CH-1211 Geneva +41 76 527 80 83 http://www.alrik-thiem.net http://www.compasss.org ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. [[alternative HTML version deleted]]
On Fri, Feb 27, 2015 at 5:19 PM, Alrik Thiem <alrik.thiem at gmail.com> wrote:> I would like to replace all lower-case letters in a string that are not part > of certain fixed expressions. For example, I have the string: > > "pmin(pmax(pmin(x1, X2), pmin(X3, X4)) == Y, pmax(Z1, z1))" > > Where I would like to replace all lower-case letters that do not belong to > the functions "pmin" and "pmax" by 1 - toupper(...) to get > > "pmin(pmax(pmin(1 - X1, X2), pmin(X3, X4)) == Y, pmax(Z1, 1 - Z1))" >Assuming x is the input string: gsub("(\\b[a-oq-z][a-z0-9]+)", "1-\\U\\1", x, perl = TRUE) ## [1] "pmin(pmax(pmin(1-X1, X2), pmin(X3, X4)) == Y, pmax(Z1, 1-Z1))" -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Dear Gabor, Many thanks. Works like a charm, but I can't get it to work with "pmin(pmax(pmin(a,B),pmin(a,C,d))==Y,pmax(E,e))" i.e., with strings where there're no integers following the components in the pmin/pmax functions. Could this be generalized to handle both cases? Best wishes, Alrik -----Urspr?ngliche Nachricht----- Von: Gabor Grothendieck [mailto:ggrothendieck at gmail.com] Gesendet: Samstag, 28. Februar 2015 13:35 An: Alrik Thiem Cc: r-help at r-project.org Betreff: Re: [R] Substring replacement in string On Fri, Feb 27, 2015 at 5:19 PM, Alrik Thiem <alrik.thiem at gmail.com> wrote:> I would like to replace all lower-case letters in a string that are not part > of certain fixed expressions. For example, I have the string: > > "pmin(pmax(pmin(x1, X2), pmin(X3, X4)) == Y, pmax(Z1, z1))" > > Where I would like to replace all lower-case letters that do not belong to > the functions "pmin" and "pmax" by 1 - toupper(...) to get > > "pmin(pmax(pmin(1 - X1, X2), pmin(X3, X4)) == Y, pmax(Z1, 1 - Z1))" >Assuming x is the input string: gsub("(\\b[a-oq-z][a-z0-9]+)", "1-\\U\\1", x, perl = TRUE) ## [1] "pmin(pmax(pmin(1-X1, X2), pmin(X3, X4)) == Y, pmax(Z1, 1-Z1))" -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Hi Alrik, With the Biostrings/IRanges infrastructure (Bioconductor packages), you can do this with: library(Biostrings) x0 <- BString("pmin(pmax(pmin(x1, X2), pmin(X3, X4)) == Y, pmax(Z1, z1))") donttouch_words <- c("pmin", "pmax") ## Extract the substrings to modify (target substrings). donttouch_regions <- reduce(do.call("c", lapply(donttouch_words, matchPattern, x0))) target_regions <- ranges(gaps(donttouch_regions)) target_substrings <- extractAt(x0, target_regions) ## Modify them. old <- paste0(letters, collapse="") new <- paste0(LETTERS, collapse="") target_substrings <- chartr(old, new, target_substrings) ## Replace in original string. x1 <- replaceAt(x0, target_regions, target_substrings) Then: > x1 57-letter "BString" instance seq: pmin(pmax(pmin(X1, X2), pmin(X3, X4)) == Y, pmax(Z1, Z1)) > as.character(x1) [1] "pmin(pmax(pmin(X1, X2), pmin(X3, X4)) == Y, pmax(Z1, Z1))" Hope this helps, H. On 02/27/2015 02:19 PM, Alrik Thiem wrote:> Dear R-help list, > > I would like to replace all lower-case letters in a string that are not part > of certain fixed expressions. For example, I have the string: > > "pmin(pmax(pmin(x1, X2), pmin(X3, X4)) == Y, pmax(Z1, z1))" > > Where I would like to replace all lower-case letters that do not belong to > the functions "pmin" and "pmax" by 1 - toupper(...) to get > > "pmin(pmax(pmin(1 - X1, X2), pmin(X3, X4)) == Y, pmax(Z1, 1 - Z1))" > > Any ideas on how I could achieve that? > > Many thanks and best wishes, > > Alrik > > > ******************************** > Alrik Thiem > Post-Doctoral Researcher > > Department of Philosophy > University of Geneva > Rue de Candolle 2 > CH-1211 Geneva > > +41 76 527 80 83 > > http://www.alrik-thiem.net > http://www.compasss.org > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >-- Herv? Pag?s Program in Computational Biology Division of Public Health Sciences Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N, M1-B514 P.O. Box 19024 Seattle, WA 98109-1024 E-mail: hpages at fredhutch.org Phone: (206) 667-5791 Fax: (206) 667-1319
Dear Herv?, Many thanks for your suggestion. Gabor Grothendieck proposed a simple one-liner that works perfectly for my purposes: gsub("(\\b[a-oq-z][a-z0-9]*)", "1-\\U\\1", x, perl = TRUE) where x is the respective string. Best wishes, Alrik -----Urspr?ngliche Nachricht----- Von: Herv? Pag?s [mailto:hpages at fredhutch.org] Gesendet: Samstag, 28. Februar 2015 23:29 An: Alrik Thiem; r-help at r-project.org Betreff: Re: [R] Substring replacement in string Hi Alrik, With the Biostrings/IRanges infrastructure (Bioconductor packages), you can do this with: library(Biostrings) x0 <- BString("pmin(pmax(pmin(x1, X2), pmin(X3, X4)) == Y, pmax(Z1, z1))") donttouch_words <- c("pmin", "pmax") ## Extract the substrings to modify (target substrings). donttouch_regions <- reduce(do.call("c", lapply(donttouch_words, matchPattern, x0))) target_regions <- ranges(gaps(donttouch_regions)) target_substrings <- extractAt(x0, target_regions) ## Modify them. old <- paste0(letters, collapse="") new <- paste0(LETTERS, collapse="") target_substrings <- chartr(old, new, target_substrings) ## Replace in original string. x1 <- replaceAt(x0, target_regions, target_substrings) Then: > x1 57-letter "BString" instance seq: pmin(pmax(pmin(X1, X2), pmin(X3, X4)) == Y, pmax(Z1, Z1)) > as.character(x1) [1] "pmin(pmax(pmin(X1, X2), pmin(X3, X4)) == Y, pmax(Z1, Z1))" Hope this helps, H. On 02/27/2015 02:19 PM, Alrik Thiem wrote:> Dear R-help list, > > I would like to replace all lower-case letters in a string that are notpart> of certain fixed expressions. For example, I have the string: > > "pmin(pmax(pmin(x1, X2), pmin(X3, X4)) == Y, pmax(Z1, z1))" > > Where I would like to replace all lower-case letters that do not belong to > the functions "pmin" and "pmax" by 1 - toupper(...) to get > > "pmin(pmax(pmin(1 - X1, X2), pmin(X3, X4)) == Y, pmax(Z1, 1 - Z1))" > > Any ideas on how I could achieve that? > > Many thanks and best wishes, > > Alrik > > > ******************************** > Alrik Thiem > Post-Doctoral Researcher > > Department of Philosophy > University of Geneva > Rue de Candolle 2 > CH-1211 Geneva > > +41 76 527 80 83 > > http://www.alrik-thiem.net > http://www.compasss.org > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. >-- Herv? Pag?s Program in Computational Biology Division of Public Health Sciences Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N, M1-B514 P.O. Box 19024 Seattle, WA 98109-1024 E-mail: hpages at fredhutch.org Phone: (206) 667-5791 Fax: (206) 667-1319