Hi all, what are the placeholders for string operations/modifications? Is there a placeholder for numbers, which would allow me to easily replace all numbers in a string? Something like text1 <- c("this is a number 23%") text2 <- c("this is not a number bla%") newtext1 <- gsub(#%, [percentagevalue], text) newtext2 <- gsub(#%, [percentagevalue], text) newtext1 should be "this is a number [percentagevalue]" newtext2 should be "this is not a number 23%" I figured there is * ? . but I can't find a source that explains their use and lists other placeholders.. Appreciate your help! Thanks Simon
Hi, ?gsub("#%", "[percentagevalue]", text1) #[1] "this is a number 23%" ?gsub("\\d+%$", "[percentagevalue]", text1) #[1] "this is a number [percentagevalue]" ?gsub("bla", "23", text2) #[1] "this is not a number 23%" A.K. ----- Original Message ----- From: Simon Pickert <simon.pickert at t-online.de> To: r-help at r-project.org Cc: Sent: Wednesday, September 4, 2013 11:17 AM Subject: [R] Placeholders for String Operations Hi all, what are the placeholders for string operations/modifications? Is there a placeholder for numbers, which would allow me to easily replace all numbers in a string? Something like text1 <- c("this is a number 23%") text2 <- c("this is not a number bla%") newtext1 <- gsub(#%, [percentagevalue], text) newtext2 <- gsub(#%, [percentagevalue], text) newtext1? should be "this is a number [percentagevalue]" newtext2? should be "this is not a number 23%" I figured there is * ? . but I can't find a source that explains their use and lists other placeholders.. Appreciate your help! Thanks Simon ______________________________________________ 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.
Hi Simon, What you need are regular expressions. The help for gsub says this, but in such a way that if you didn't know that's what you were looking for, you wouldn't learn it there: See the help pages on regular expression for details of the different types of regular expressions. The See Also section has a better clue: regular expression (aka ?regexp?) for the details of the pattern specification. ?regexp has a fairly terse explanation. I'd look at some of the many guides to regular expressions online, and use ?regexp mainly for how the R implementation differs from standard (mostly in the use of \). The help page does list all the groups, which is what you wanted. Sarah On Wed, Sep 4, 2013 at 11:17 AM, Simon Pickert <simon.pickert at t-online.de> wrote:> Hi all, > > what are the placeholders for string operations/modifications? Is there a placeholder for numbers, which would allow me to easily replace all numbers in a string? Something like > > text1 <- c("this is a number 23%") > text2 <- c("this is not a number bla%") > > newtext1 <- gsub(#%, [percentagevalue], text) > newtext2 <- gsub(#%, [percentagevalue], text) > > > newtext1 should be "this is a number [percentagevalue]" > newtext2 should be "this is not a number 23%" > > > I figured there is * ? . but I can't find a source that explains their use and lists other placeholders.. > > > Appreciate your help! > Thanks > Simon-- Sarah Goslee http://www.functionaldiversity.org
Hello, I'm not sure I understand, but if you want a ?regexp to only match numbers before a %, try the following. gsub("[0-9]+%", "[percentagevalue]", text1) gsub("[0-9]+%", "[percentagevalue]", text2) [0-9] matches any character in the range from 0 to 9, and the + means to repeat that character any number of times. See the help page for ?regexp. Hope this helps, Rui Barradas Em 04-09-2013 16:17, Simon Pickert escreveu:> Hi all, > > what are the placeholders for string operations/modifications? Is there a placeholder for numbers, which would allow me to easily replace all numbers in a string? Something like > > text1 <- c("this is a number 23%") > text2 <- c("this is not a number bla%") > > newtext1 <- gsub(#%, [percentagevalue], text) > newtext2 <- gsub(#%, [percentagevalue], text) > > > newtext1 should be "this is a number [percentagevalue]" > newtext2 should be "this is not a number 23%" > > > I figured there is * ? . but I can't find a source that explains their use and lists other placeholders.. > > > Appreciate your help! > Thanks > Simon > ______________________________________________ > 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. >