Re: [R] reg expr that retains only bracketed text from strings
Hi Nevil, In case you are still having trouble with this, I wrote something in R that should do what you want: mystrings<-c("ABC","A(B)C","AB[C]","BC","{AB}C") get_enclosed<-function(x,left=c("(","[","<","{"),right=c(")","]",">","}")) { newx<-rep("",length(x)) for(li in 1:length(left)) { for(xi in 1:length(x)) { lp<-regexpr(left[li],x[xi],fixed=TRUE) rp<-regexpr(right[li],x[xi],fixed=TRUE) if(lp > 0 && rp > 0) newx[xi]<-substr(x[xi],lp+1,rp-1) } } return(newx) } get_enclosed(mystrings) Jim On Thu, Jun 13, 2019 at 12:32 AM William Dunlap via R-help wrote: > > strcapture() can help here. > > > mystrings<-c("ABC","A(B)C","AB(C)") > > strcapture("^[^{]*(\\([^(]*\\)).*$", mystrings, > proto=data.frame(InParen="")) > InParen > 1 > 2 (B) > 3 (C) > > Classic regular expressions don't do so well with nested parentheses. > Perhaps a perl-style RE could do that. > > strcapture("^[^{]*(\\([^(]*\\)).*$", proto=data.frame(InParen=""), > x=c("()", "a(s(d)f)g")) > InParen > 1 () > 2 (d)f) > > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > > On Tue, Jun 11, 2019 at 10:46 PM nevil amos wrote: > > > Hi > > > > I am trying to extract only the text contained in brackets from a vector of > > strings > > not all of the strings contain closed bracketed text, they should return an > > empty string or NA > > > > this is what I have at the moment > > > > > > mystrings<-c("ABC","A(B)C","AB(C)") > > > > substring(mystrings, regexpr("\\(|\\)", mystrings)) > > > > > > #this returns the whole string if there are no brackets. > > [1] "ABC" "(B)C" "(C)" > > > > > > # my desired desired output: > > #[1] "" "(B)" "(C)" > > > > many thanks for any suggestions > > Nevil Amos > > > > [[alternative HTML version deleted]] > > > > __ > > R-help@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]] > > __ > R-help@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. __ R-help@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.
Re: [R] reg expr that retains only bracketed text from strings
strcapture() can help here. > mystrings<-c("ABC","A(B)C","AB(C)") > strcapture("^[^{]*(\\([^(]*\\)).*$", mystrings, proto=data.frame(InParen="")) InParen 1 2 (B) 3 (C) Classic regular expressions don't do so well with nested parentheses. Perhaps a perl-style RE could do that. > strcapture("^[^{]*(\\([^(]*\\)).*$", proto=data.frame(InParen=""), x=c("()", "a(s(d)f)g")) InParen 1 () 2 (d)f) Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Jun 11, 2019 at 10:46 PM nevil amos wrote: > Hi > > I am trying to extract only the text contained in brackets from a vector of > strings > not all of the strings contain closed bracketed text, they should return an > empty string or NA > > this is what I have at the moment > > > mystrings<-c("ABC","A(B)C","AB(C)") > > substring(mystrings, regexpr("\\(|\\)", mystrings)) > > > #this returns the whole string if there are no brackets. > [1] "ABC" "(B)C" "(C)" > > > # my desired desired output: > #[1] "" "(B)" "(C)" > > many thanks for any suggestions > Nevil Amos > > [[alternative HTML version deleted]] > > __ > R-help@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]] __ R-help@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.
Re: [R] reg expr that retains only bracketed text from strings
On Wed, 12 Jun 2019 15:45:04 +1000 nevil amos wrote: > # my desired desired output: > #[1] "" "(B)" "(C)" (function(s) regmatches( s, gregexpr('\\([^)]+\\)', s) ))(c("ABC","A(B)C","AB(C)")) # [[1]] # character(0) # # [[2]] # [1] "(B)" # # [[3]] # [1] "(C)" This matches all substrings that start with an ( and are followed by non-zero amount of non-) characters, then terminated by ). If there are multiple such substrings, all are returned. -- Best regards, Ivan __ R-help@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.
Re: [R] reg expr that retains only bracketed text from strings
Hi Nevil, Here's one way to do it. (No doubt some regular-expression-gurus will have more concise ways to get the job done.) a1 <- sub(".*\\(","\\(",mystrings) a2 <- sub("\\).*","\\)",a1) a2[grep("\\(",a2,invert=TRUE)] <- "" a2 HTH, Eric On Wed, Jun 12, 2019 at 8:46 AM nevil amos wrote: > Hi > > I am trying to extract only the text contained in brackets from a vector of > strings > not all of the strings contain closed bracketed text, they should return an > empty string or NA > > this is what I have at the moment > > > mystrings<-c("ABC","A(B)C","AB(C)") > > substring(mystrings, regexpr("\\(|\\)", mystrings)) > > > #this returns the whole string if there are no brackets. > [1] "ABC" "(B)C" "(C)" > > > # my desired desired output: > #[1] "" "(B)" "(C)" > > many thanks for any suggestions > Nevil Amos > > [[alternative HTML version deleted]] > > __ > R-help@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]] __ R-help@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.
[R] reg expr that retains only bracketed text from strings
Hi I am trying to extract only the text contained in brackets from a vector of strings not all of the strings contain closed bracketed text, they should return an empty string or NA this is what I have at the moment mystrings<-c("ABC","A(B)C","AB(C)") substring(mystrings, regexpr("\\(|\\)", mystrings)) #this returns the whole string if there are no brackets. [1] "ABC" "(B)C" "(C)" # my desired desired output: #[1] "" "(B)" "(C)" many thanks for any suggestions Nevil Amos [[alternative HTML version deleted]] __ R-help@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.
Re: [R] eval(expr) without printing to screen?
baptiste auguie wrote: Hi, What about this, eval(parse(text=expr)) (no print) HTH, baptiste Thanks. For some reason I couldn't think of that, for some reason I had a dim memory in my head that that wouldn't work, but it does. Thanks! Cheers, Nick 2009/9/19 Nick Matzke mat...@berkeley.edu: Hi, I have a script which I source, which evaluates a changing expression call hundreds of times. It works, but it prints to screen each time, which is annoying. There must be simple way to suppress this, or to use a slightly different set of commands, which will be obvious to those wiser than I... Here is a simpler mockup which shows the issue: x = data.frame(rbind(c(1,2,3),c(1,2,3))) xnames = c(a, b, c) names(x) = xnames for(i in 1:length(x)) { # Create a varying string expression expr = paste(y = x$, xnames[i], [1], sep=) # evaluate expression eval(parse(text=print(expr))) # This command prints the expression to screen even when embedded in a function in a sourced script. I would prefer it didn't! } PS: I have to go through this rigamarole: expr = y1 = x$c[1] eval(parse(text=print(expr))) Because the following doesn't work, even though it seems like it should: expr = y = x$c[2] eval(expr) -- Nicholas J. Matzke Ph.D. Candidate, Graduate Student Researcher Huelsenbeck Lab Center for Theoretical Evolutionary Genomics 4151 VLSB (Valley Life Sciences Building) Department of Integrative Biology University of California, Berkeley Lab websites: http://ib.berkeley.edu/people/lab_detail.php?lab=54 http://fisher.berkeley.edu/cteg/hlab.html Dept. personal page: http://ib.berkeley.edu/people/students/person_detail.php?person=370 Lab personal page: http://fisher.berkeley.edu/cteg/members/matzke.html Lab phone: 510-643-6299 Dept. fax: 510-643-6264 Cell phone: 510-301-0179 Email: mat...@berkeley.edu Mailing address: Department of Integrative Biology 3060 VLSB #3140 Berkeley, CA 94720-3140 - [W]hen people thought the earth was flat, they were wrong. When people thought the earth was spherical, they were wrong. But if you think that thinking the earth is spherical is just as wrong as thinking the earth is flat, then your view is wronger than both of them put together. Isaac Asimov (1989). The Relativity of Wrong. The Skeptical Inquirer, 14(1), 35-44. Fall 1989. http://chem.tufts.edu/AnswersInScience/RelativityofWrong.htm __ R-help@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. -- Nicholas J. Matzke Ph.D. Candidate, Graduate Student Researcher Huelsenbeck Lab Center for Theoretical Evolutionary Genomics 4151 VLSB (Valley Life Sciences Building) Department of Integrative Biology University of California, Berkeley Lab websites: http://ib.berkeley.edu/people/lab_detail.php?lab=54 http://fisher.berkeley.edu/cteg/hlab.html Dept. personal page: http://ib.berkeley.edu/people/students/person_detail.php?person=370 Lab personal page: http://fisher.berkeley.edu/cteg/members/matzke.html Lab phone: 510-643-6299 Dept. fax: 510-643-6264 Cell phone: 510-301-0179 Email: mat...@berkeley.edu Mailing address: Department of Integrative Biology 3060 VLSB #3140 Berkeley, CA 94720-3140 - [W]hen people thought the earth was flat, they were wrong. When people thought the earth was spherical, they were wrong. But if you think that thinking the earth is spherical is just as wrong as thinking the earth is flat, then your view is wronger than both of them put together. Isaac Asimov (1989). The Relativity of Wrong. The Skeptical Inquirer, 14(1), 35-44. Fall 1989. http://chem.tufts.edu/AnswersInScience/RelativityofWrong.htm __ R-help@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.
Re: [R] eval(expr) without printing to screen?
Here is a simpler mockup which shows the issue: x = data.frame(rbind(c(1,2,3),c(1,2,3))) xnames = c(a, b, c) names(x) = xnames for(i in 1:length(x)) { # Create a varying string expression expr = paste(y = x$, xnames[i], [1], sep=) # evaluate expression eval(parse(text=print(expr))) # This command prints the expression to screen even when embedded in a function in a sourced script. I would prefer it didn't! } Why are you using eval? The following is equivalent: for(name in names(x)) { y - x[[name]][1] } Hadley -- http://had.co.nz/ __ R-help@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] eval(expr) without printing to screen?
Hi, I have a script which I source, which evaluates a changing expression call hundreds of times. It works, but it prints to screen each time, which is annoying. There must be simple way to suppress this, or to use a slightly different set of commands, which will be obvious to those wiser than I... Here is a simpler mockup which shows the issue: x = data.frame(rbind(c(1,2,3),c(1,2,3))) xnames = c(a, b, c) names(x) = xnames for(i in 1:length(x)) { # Create a varying string expression expr = paste(y = x$, xnames[i], [1], sep=) # evaluate expression eval(parse(text=print(expr))) # This command prints the expression to screen even when embedded in a function in a sourced script. I would prefer it didn't! } PS: I have to go through this rigamarole: expr = y1 = x$c[1] eval(parse(text=print(expr))) Because the following doesn't work, even though it seems like it should: expr = y = x$c[2] eval(expr) -- Nicholas J. Matzke Ph.D. Candidate, Graduate Student Researcher Huelsenbeck Lab Center for Theoretical Evolutionary Genomics 4151 VLSB (Valley Life Sciences Building) Department of Integrative Biology University of California, Berkeley Lab websites: http://ib.berkeley.edu/people/lab_detail.php?lab=54 http://fisher.berkeley.edu/cteg/hlab.html Dept. personal page: http://ib.berkeley.edu/people/students/person_detail.php?person=370 Lab personal page: http://fisher.berkeley.edu/cteg/members/matzke.html Lab phone: 510-643-6299 Dept. fax: 510-643-6264 Cell phone: 510-301-0179 Email: mat...@berkeley.edu Mailing address: Department of Integrative Biology 3060 VLSB #3140 Berkeley, CA 94720-3140 - [W]hen people thought the earth was flat, they were wrong. When people thought the earth was spherical, they were wrong. But if you think that thinking the earth is spherical is just as wrong as thinking the earth is flat, then your view is wronger than both of them put together. Isaac Asimov (1989). The Relativity of Wrong. The Skeptical Inquirer, 14(1), 35-44. Fall 1989. http://chem.tufts.edu/AnswersInScience/RelativityofWrong.htm __ R-help@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.
Re: [R] eval(expr) without printing to screen?
Hi, What about this, eval(parse(text=expr)) (no print) HTH, baptiste 2009/9/19 Nick Matzke mat...@berkeley.edu: Hi, I have a script which I source, which evaluates a changing expression call hundreds of times. It works, but it prints to screen each time, which is annoying. There must be simple way to suppress this, or to use a slightly different set of commands, which will be obvious to those wiser than I... Here is a simpler mockup which shows the issue: x = data.frame(rbind(c(1,2,3),c(1,2,3))) xnames = c(a, b, c) names(x) = xnames for(i in 1:length(x)) { # Create a varying string expression expr = paste(y = x$, xnames[i], [1], sep=) # evaluate expression eval(parse(text=print(expr))) # This command prints the expression to screen even when embedded in a function in a sourced script. I would prefer it didn't! } PS: I have to go through this rigamarole: expr = y1 = x$c[1] eval(parse(text=print(expr))) Because the following doesn't work, even though it seems like it should: expr = y = x$c[2] eval(expr) -- Nicholas J. Matzke Ph.D. Candidate, Graduate Student Researcher Huelsenbeck Lab Center for Theoretical Evolutionary Genomics 4151 VLSB (Valley Life Sciences Building) Department of Integrative Biology University of California, Berkeley Lab websites: http://ib.berkeley.edu/people/lab_detail.php?lab=54 http://fisher.berkeley.edu/cteg/hlab.html Dept. personal page: http://ib.berkeley.edu/people/students/person_detail.php?person=370 Lab personal page: http://fisher.berkeley.edu/cteg/members/matzke.html Lab phone: 510-643-6299 Dept. fax: 510-643-6264 Cell phone: 510-301-0179 Email: mat...@berkeley.edu Mailing address: Department of Integrative Biology 3060 VLSB #3140 Berkeley, CA 94720-3140 - [W]hen people thought the earth was flat, they were wrong. When people thought the earth was spherical, they were wrong. But if you think that thinking the earth is spherical is just as wrong as thinking the earth is flat, then your view is wronger than both of them put together. Isaac Asimov (1989). The Relativity of Wrong. The Skeptical Inquirer, 14(1), 35-44. Fall 1989. http://chem.tufts.edu/AnswersInScience/RelativityofWrong.htm __ R-help@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@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.
Re: [R] eval(expr) without printing to screen?
On Sep 19, 2009, at 4:48 PM, Nick Matzke wrote: Hi, I have a script which I source, which evaluates a changing expression call hundreds of times. It works, but it prints to screen each time, which is annoying. There must be simple way to suppress this, or to use a slightly different set of commands, which will be obvious to those wiser than I... Here is a simpler mockup which shows the issue: x = data.frame(rbind(c(1,2,3),c(1,2,3))) xnames = c(a, b, c) names(x) = xnames for(i in 1:length(x)) { # Create a varying string expression expr = paste(y = x$, xnames[i], [1], sep=) # evaluate expression eval(parse(text=print(expr))) Why are you printing expr? Seems that you are making it difficult to achieve your goal of quiet execution if you print the expressions inside the parse function. # This command prints the expression to screen even when embedded in a function in a sourced script. I would prefer it didn't! } PS: I have to go through this rigamarole: expr = y1 = x$c[1] eval(parse(text=print(expr))) Because the following doesn't work, even though it seems like it should: expr = y = x$c[2] eval(expr) -- David Winsemius, MD Heritage Laboratories West Hartford, CT __ R-help@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.
Re: [R] eval(expr) without printing to screen?
David, You can used the sink function to direct the output to a file. When you do this, nothing is printed on the screen. e.g. sink(c:\\RResu.txt) #Defines location to which output is to be written #R code goes here sink() #Turns off redirection, any code after this will print to the screen John John David Sorkin M.D., Ph.D. Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing) David Winsemius dwinsem...@comcast.net 9/19/2009 5:01 PM On Sep 19, 2009, at 4:48 PM, Nick Matzke wrote: Hi, I have a script which I source, which evaluates a changing expression call hundreds of times. It works, but it prints to screen each time, which is annoying. There must be simple way to suppress this, or to use a slightly different set of commands, which will be obvious to those wiser than I... Here is a simpler mockup which shows the issue: x = data.frame(rbind(c(1,2,3),c(1,2,3))) xnames = c(a, b, c) names(x) = xnames for(i in 1:length(x)) { # Create a varying string expression expr = paste(y = x$, xnames[i], [1], sep=) # evaluate expression eval(parse(text=print(expr))) Why are you printing expr? Seems that you are making it difficult to achieve your goal of quiet execution if you print the expressions inside the parse function. # This command prints the expression to screen even when embedded in a function in a sourced script. I would prefer it didn't! } PS: I have to go through this rigamarole: expr = y1 = x$c[1] eval(parse(text=print(expr))) Because the following doesn't work, even though it seems like it should: expr = y = x$c[2] eval(expr) -- David Winsemius, MD Heritage Laboratories West Hartford, CT __ R-help@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. Confidentiality Statement: This email message, including any attachments, is for th...{{dropped:6}} __ R-help@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.
Re: [R] if ( expr )
How about if (is.na(c(x,1))[1]) ... --- Gregory Wall [EMAIL PROTECTED] wrote: Hello, I've searched the list but haven't found anything really applicable to my question. Any advice would be super. I'm working on a snippet of R code and I have a function with a prototype like this: foo - function( x, ... ){ if( is.na(x)[1] ) {etc...} } Where x is typically a vector of bools. At times, however, x can be NA, and yet at other super rare times x can be the result of this type of comparison: c(4,5,13,2,3,4,5,7) == numeric(0) which produces logical(0). When this is the case, if( is.na(x)[1] ) isn't happy. I'm trying to create a condition to my if statement inside foo that only is true when x is NA but that doesn't die when its a logical(0). Any suggestions on how best to approach this? As always, thanks a bunch, Greg __ R-help@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@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] if ( expr )
Hello, I've searched the list but haven't found anything really applicable to my question. Any advice would be super. I'm working on a snippet of R code and I have a function with a prototype like this: foo - function( x, ... ){ if( is.na(x)[1] ) {etc...} } Where x is typically a vector of bools. At times, however, x can be NA, and yet at other super rare times x can be the result of this type of comparison: c(4,5,13,2,3,4,5,7) == numeric(0) which produces logical(0). When this is the case, if( is.na(x)[1] ) isn't happy. I'm trying to create a condition to my if statement inside foo that only is true when x is NA but that doesn't die when its a logical(0). Any suggestions on how best to approach this? As always, thanks a bunch, Greg __ R-help@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.