Dear all, I want to manipulate a character string such as ex<-"cbind(data$response1,data$response2)" in R in two ways: 1) extracting the "response1" portion of ex 2) replacing "$" with "." I am wondering that is it possible efficiently doing these in R? Best Ozgur -- View this message in context: http://r.789695.n4.nabble.com/two-questions-about-character-manipulation-tp4643292.html Sent from the R help mailing list archive at Nabble.com.
Hello, Try the following. 1) pattern <- "response." m <- regexpr(pattern, ex) #gregexpr to get all "response" regmatches(ex, m) 2) gsub("\\$", "\\.", ex) Hope this helps, Rui Barradas Em 16-09-2012 15:35, ?zg?r Asar escreveu:> Dear all, > > I want to manipulate a character string such as > > ex<-"cbind(data$response1,data$response2)" > > in R in two ways: > > 1) extracting the "response1" portion of ex > 2) replacing "$" with "." > > I am wondering that is it possible efficiently doing these in R? > > Best > > Ozgur > > > > -- > View this message in context: http://r.789695.n4.nabble.com/two-questions-about-character-manipulation-tp4643292.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
On Sun, Sep 16, 2012 at 3:35 PM, ?zg?r Asar <oasar at metu.edu.tr> wrote:> Dear all, > > I want to manipulate a character string such as > > ex<-"cbind(data$response1,data$response2)" > > in R in two ways: > > 1) extracting the "response1" portion of exI'm not sure what you mean by "portion" -- if you just want "response1" why do you need to process ex? You probably wind up wanting to use gsub() and putting in "" for things which aren't response1, but again, it seems impractical...> 2) replacing "$" with "."gsub("$", ".", ex, fixed = TRUE) Cheers, Michael> > I am wondering that is it possible efficiently doing these in R? > > Best > > Ozgur > > > > -- > View this message in context: http://r.789695.n4.nabble.com/two-questions-about-character-manipulation-tp4643292.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Dear Rui Barradas and Michael Weylandt, Many thanks for your replies. My second question is solved now. But I think I did not expressed my first wish in a clear way Indeed, in ex<-"cbind(data$response1,data$response2), I want to extract the variable name between "$" and "," (corresponds to response1 in this example) and the one between "$" and ")" (corresponds to response2). These symbols ("$", ",", ")" ) are always same, but the names (response1, response2) might change from data to data. Best Ozgur -- View this message in context: http://r.789695.n4.nabble.com/two-questions-about-character-manipulation-tp4643292p4643301.html Sent from the R help mailing list archive at Nabble.com.
Hi, Try this: ex<-"cbind(data$response1,data$response2)" ?gsub(".*\\(.*\\$(.*)\\,.*\\$.*\\)","\\1",ex) #[1] "response1" unlist(strsplit(gsub(".*\\(.*\\$(.*)\\,.*\\$(.*)\\)","\\1 \\2",ex)," ")) #[1] "response1" "response2" A.K. ----- Original Message ----- From: ?zg?r Asar <oasar at metu.edu.tr> To: r-help at r-project.org Cc: Sent: Sunday, September 16, 2012 12:26 PM Subject: Re: [R] two questions about character manipulation Dear Rui Barradas and Michael Weylandt, Many thanks for your replies. My second question is solved now. But I think I did not expressed my first wish in a clear way Indeed, in ex<-"cbind(data$response1,data$response2), I want to extract the variable name between "$" and "," (corresponds to response1 in this example) and the one between "$" and ")" (corresponds to response2). These symbols ("$", ",", ")" ) are always same, but the names (response1, response2) might change from data to data. Best Ozgur -- View this message in context: http://r.789695.n4.nabble.com/two-questions-about-character-manipulation-tp4643292p4643301.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.