Skip to content
Prev 298894 / 398503 Next

R sub query

On Jul 2, 2012, at 4:15 AM, Sarah Auburn wrote:

            
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 ':'.)