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
Dear Alrik This may seem a silly suggestion but why not just define new functions PMIN and PMAX to call pmin and pmax. Obviously that does not solve your problem if it is more general than your example. On 28/02/2015 13:16, Alrik Thiem wrote:> 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))" > > >-- Michael http://www.dewey.myzen.co.uk
Dear Michael I'm not sure how you mean this. Maybe a more general description of my problem is helpful for clarifying. What I have to deal with are truth table output functions that always take, for example, the following form: Delta <- "(a*B+a*C*d<=>Y)*(E+e)" I.e. these functions will always have the structure (.*.+.*.+...<=>.)*(.+.), where the dots in the antecedent could be further conjunctions of unspecified complexity. I now need to filter all rows from the truth table that are compatible with this output function. To create the input part of the truth table "tt" for Delta above, I do: library(QCA) # createMatrix() function is part of this package Delta.upper <- toupper(Delta) f.names <- unique(unlist(strsplit(Delta.upper, "[(*+<=>)]"))) f.names <- f.names[f.names != ""] tt <- data.frame(createMatrix(rep(2, length(f.names)))) dimnames(tt) <- list(as.character(seq(2^length(f.names))), f.names) tt> ttA B C D Y E 1 0 0 0 0 0 0 2 0 0 0 0 0 1 . . . . . . . 63 1 1 1 1 1 0 64 1 1 1 1 1 1 I now need to transform Delta into a string of the following form in order to extract the subset of compatible rows from "tt": "pmin(pmax(pmin(1-tt$A,tt$B),pmin(1-tt$A,tt$C,1-tt$D))==tt$Y,pmax(tt$E,1-tt$E))==TRUE" With this string, I can then do:> tt[pmin(pmax(pmin(1-tt$A,tt$B), pmin(1-tt$A,tt$C,1-tt$D))==tt$Y,pmax(tt$E,1-tt$E))==TRUE, ]A B C D Y E 1 0 0 0 0 0 0 2 0 0 0 0 0 1 . . . . . . . 61 1 1 1 1 0 0 62 1 1 1 1 0 1 -----Urspr?ngliche Nachricht----- Von: Michael Dewey [mailto:info at aghmed.fsnet.co.uk] Gesendet: Samstag, 28. Februar 2015 14:50 An: Alrik Thiem Cc: r-help at r-project.org Betreff: Re: [R] Substring replacement in string Dear Alrik This may seem a silly suggestion but why not just define new functions PMIN and PMAX to call pmin and pmax. Obviously that does not solve your problem if it is more general than your example. On 28/02/2015 13:16, Alrik Thiem wrote:> 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))" > > >-- Michael http://www.dewey.myzen.co.uk
Replace the + (i.e. 1 or more) in the pattern with a * (i.e. 0 or more): x <- "pmin(pmax(pmin(a,B),pmin(a,C,d))==Y,pmax(E,e))" gsub("(\\b[a-oq-z][a-z0-9]*)", "1-\\U\\1", x, perl = TRUE) giving: [1] "pmin(pmax(pmin(1-A,B),pmin(1-A,C,1-D))==Y,pmax(E,1-E))" Here is a visualization of the regular expression: https://www.debuggex.com/i/5ByOCQS2zIdPEf-f.png On Sat, Feb 28, 2015 at 8:16 AM, Alrik Thiem <alrik.thiem at gmail.com> wrote:> 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 >-- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Dear Gabor, That works perfectly! Many thanks and best wishes, Alrik -----Urspr?ngliche Nachricht----- Von: Gabor Grothendieck [mailto:ggrothendieck at gmail.com] Gesendet: Samstag, 28. Februar 2015 19:30 An: Alrik Thiem Cc: r-help at r-project.org Betreff: Re: [R] Substring replacement in string Replace the + (i.e. 1 or more) in the pattern with a * (i.e. 0 or more): x <- "pmin(pmax(pmin(a,B),pmin(a,C,d))==Y,pmax(E,e))" gsub("(\\b[a-oq-z][a-z0-9]*)", "1-\\U\\1", x, perl = TRUE) giving: [1] "pmin(pmax(pmin(1-A,B),pmin(1-A,C,1-D))==Y,pmax(E,1-E))" Here is a visualization of the regular expression: https://www.debuggex.com/i/5ByOCQS2zIdPEf-f.png On Sat, Feb 28, 2015 at 8:16 AM, Alrik Thiem <alrik.thiem at gmail.com> wrote:> 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 >-- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com