An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121213/54b5f55b/attachment.pl>
replace parenthetical phrases in a string
3 messages · Adams, Jean, arun
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121213/c7e7b0bd/attachment.pl>
Hi,
I guess there are some problems with spaces in this solution.
?y
[1] "My toast=bog? and eggs omit=32" "dogs have ears"???????????????
[3] "cats have tails? "????????????
?gsub(" *\\([^)]*\\) *", "", x)
#[1] "My toast=bogand eggsomit=32" "dogs have ears"????????????
#[3] "cats have tails"??
You could try this:
?gsub("(\\(.*\\))+?"," ",x)
#[1] "My toast=bog? and eggs omit=32" "dogs have ears"???????????????
#[3] "cats have tails? "????????
A.K.
----- Original Message -----
From: "Adams, Jean" <jvadams at usgs.gov>
To: r-help at r-project.org
Cc:
Sent: Thursday, December 13, 2012 6:03 PM
Subject: Re: [R] replace parenthetical phrases in a string
My apologies.? I sent too soon!
I did a bit more digging, and found a solution on the R-help archives.
y <- gsub(" *\\([^)]*\\) *", "", x)
Jean
On Thu, Dec 13, 2012 at 4:53 PM, Adams, Jean <jvadams at usgs.gov> wrote:
R-helpers,
I have a vector of character strings in which I would like to replace each
parenthetical phrase with a single space, " ".? For example if I start with
x, I would like to end up with y.
x <- c("My toast=bog(keep=3 no=4) and eggs(er34)omit=32",
"dogs have ears",
"cats have tails (and ears, too!)")
y <- c("My toast=bog? and eggs omit=32",
"dogs have ears",
"cats have tails? ")
I'm guessing that this can be done with gsub(), but I have never mastered
the mysteries of regular expressions.
I would greatly appreciate any pointers.
Thanks.
Jean
P.S.? I'm using R version 2.15.2 on Windows 7.
??? [[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.