|
Hello,
I would like to substitute a substring of characters defined by a specific start and end sequence. i.e. in the example matrix below, I would like to substitute ".:X:" with "", where X varies in sequence... m<-matrix(c(".:0:0,0", ".:2:0,2", ".:194:193,1", ".:56:0,56", ".:58:50,8", ".:13:0,13", ".:114:114,0", ".:75:75,0"), nrow=2) output required: [,1] [,2] [,3] [,4] [1,] "0,0" "193,1" "50,8" "114,0" [2,] "0,2" "0,56" "0,13" "75,0" Thank you for any help Sarah [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. |
|
Will this do:
> m<-matrix(c(".:0:0,0", ".:2:0,2", ".:194:193,1", ".:56:0,56", ".:58:50,8", ".:13:0,13", ".:114:114,0", ".:75:75,0"), nrow=2) > > m [,1] [,2] [,3] [,4] [1,] ".:0:0,0" ".:194:193,1" ".:58:50,8" ".:114:114,0" [2,] ".:2:0,2" ".:56:0,56" ".:13:0,13" ".:75:75,0" > sub("^\\.:[^:]*:", "", m) [,1] [,2] [,3] [,4] [1,] "0,0" "193,1" "50,8" "114,0" [2,] "0,2" "0,56" "0,13" "75,0" > On Mon, Jul 2, 2012 at 4:15 AM, Sarah Auburn <[hidden email]> wrote: > Hello, > I would like to substitute a substring of characters defined by a specific start and end sequence. > i.e. in the example matrix below, I would like to substitute ".:X:" with "", where X varies in sequence... > > m<-matrix(c(".:0:0,0", ".:2:0,2", ".:194:193,1", ".:56:0,56", ".:58:50,8", ".:13:0,13", ".:114:114,0", ".:75:75,0"), nrow=2) > > output required: > [,1] [,2] [,3] [,4] > [1,] "0,0" "193,1" "50,8" "114,0" > [2,] "0,2" "0,56" "0,13" "75,0" > > Thank you for any help > Sarah > [[alternative HTML version deleted]] > > > ______________________________________________ > [hidden email] 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. > -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. ______________________________________________ [hidden email] 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. |
|
In reply to this post by burgundy
Hi
I am not at all an expert in regular expressions but gsub("^[[:punct:]]+[[:digit:]]+:", "",m) does the output you want. Maybe by chance :-) Regards Petr > > Hello, > I would like to substitute a substring of characters defined by a specific > start and end sequence. > i.e. in the example matrix below, I would like to substitute ".:X:" with > "", where X varies in sequence... > > m<-matrix(c(".:0:0,0", ".:2:0,2", ".:194:193,1", ".:56:0,56", ".:58:50,8", > ".:13:0,13", ".:114:114,0", ".:75:75,0"), nrow=2) > > output required: > [,1] [,2] [,3] [,4] > [1,] "0,0" "193,1" "50,8" "114,0" > [2,] "0,2" "0,56" "0,13" "75,0" > > Thank you for any help > Sarah > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] 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. |
|
In reply to this post by burgundy
On Jul 2, 2012, at 4:15 AM, Sarah Auburn wrote: > Hello, > I would like to substitute a substring of characters defined by a > specific start and end sequence. > i.e. in the example matrix below, I would like to substitute ".:X:" > with "", where X varies in sequence... > > m<-matrix(c(".:0:0,0", ".:2:0,2", ".:194:193,1", ".:56:0,56", ".: > 58:50,8", ".:13:0,13", ".:114:114,0", ".:75:75,0"), nrow=2) sub("\\..+\\:", "", m) [,1] [,2] [,3] [,4] [1,] "0,0" "193,1" "50,8" "114,0" [2,] "0,2" "0,56" "0,13" "75,0" You should also look at Holtman's since he is better at this than I am but I didn't really understand how his version worked. Mine is really in three parts. The first entry '\\.' matches the leading dot and it could have been '^\\.' to avoid any confusion with decimal points. The second entry is '.+' which is anything until the third entry '\\:' which ends up matching the last ':' since these are greedy expressions. You could also have done it with "\\.\\:.+\\:" (Now that I look at his again "^\\.:[^:]*:" , I find that I can learn something from it, as often happens when I read his contributions. To my surprise the ':' character does not need to be escaped but can be and the interior of his expression '[^:]' is a negative character- class. It matches anything other than ':' and the '*' following it lets that anything be of any length. And then he didn't need to escape the trailing ':'.) -- David. > > output required: > [,1] [,2] [,3] [,4] > [1,] "0,0" "193,1" "50,8" "114,0" > [2,] "0,2" "0,56" "0,13" "75,0" > > Thank you for any help > Sarah > [[alternative HTML version deleted]] -- David Winsemius, MD West Hartford, CT ______________________________________________ [hidden email] 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. |
|
In reply to this post by burgundy
Hi,
Either of these should work: m<-matrix(c(".:0:0,0", ".:2:0,2", ".:194:193,1", ".:56:0,56", ".:58:50,8", ".:13:0,13", ".:114:114,0", ".:75:75,0"), nrow=2) gsub("^\\.:[[:digit:]]+:","",m) [,1] [,2] [,3] [,4] [1,] "0,0" "193,1" "50,8" "114,0" [2,] "0,2" "0,56" "0,13" "75,0" gsub("^\\.:\\d+:","",m) [,1] [,2] [,3] [,4] [1,] "0,0" "193,1" "50,8" "114,0" [2,] "0,2" "0,56" "0,13" "75,0" A.K. ----- Original Message ----- From: Sarah Auburn <[hidden email]> To: "[hidden email]" <[hidden email]> Cc: Sent: Monday, July 2, 2012 4:15 AM Subject: [R] R sub query Hello, I would like to substitute a substring of characters defined by a specific start and end sequence. i.e. in the example matrix below, I would like to substitute ".:X:" with "", where X varies in sequence... m<-matrix(c(".:0:0,0", ".:2:0,2", ".:194:193,1", ".:56:0,56", ".:58:50,8", ".:13:0,13", ".:114:114,0", ".:75:75,0"), nrow=2) output required: [,1] [,2] [,3] [,4] [1,] "0,0" "193,1" "50,8" "114,0" [2,] "0,2" "0,56" "0,13" "75,0" Thank you for any help Sarah [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. ______________________________________________ [hidden email] 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. |
|
Thanks everyone for your help. All of the solutions posted (Arun, David, Jim and Petr) worked for my problem.
From: arun <[hidden email]> To: Sarah Auburn <[hidden email]> Cc: R help <[hidden email]> Sent: Tuesday, 3 July 2012, 2:13 Subject: Re: [R] R sub query Hi, Either of these should work: m<-matrix(c(".:0:0,0", ".:2:0,2", ".:194:193,1", ".:56:0,56", ".:58:50,8", ".:13:0,13", ".:114:114,0", ".:75:75,0"), nrow=2) gsub("^\\.:[[:digit:]]+:","",m) [,1] [,2] [,3] [,4] [1,] "0,0" "193,1" "50,8" "114,0" [2,] "0,2" "0,56" "0,13" "75,0" gsub("^\\.:\\d+:","",m) [,1] [,2] [,3] [,4] [1,] "0,0" "193,1" "50,8" "114,0" [2,] "0,2" "0,56" "0,13" "75,0" A.K. ----- Original Message ----- From: Sarah Auburn <[hidden email]> To: "[hidden email]" <[hidden email]> Cc: Sent: Monday, July 2, 2012 4:15 AM Subject: [R] R sub query Hello, I would like to substitute a substring of characters defined by a specific start and end sequence. i.e. in the example matrix below, I would like to substitute ".:X:" with "", where X varies in sequence... m<-matrix(c(".:0:0,0", ".:2:0,2", ".:194:193,1", ".:56:0,56", ".:58:50,8", ".:13:0,13", ".:114:114,0", ".:75:75,0"), nrow=2) output required: [,1] [,2] [,3] [,4] [1,] "0,0" "193,1" "50,8" "114,0" [2,] "0,2" "0,56" "0,13" "75,0" Thank you for any help Sarah [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. |
| Powered by Nabble | Edit this page |
