Skip to content
Back to formatted view

Raw Message

Message-ID: <loom.20050818T183647-236@post.gmane.org>
Date: 2005-08-18T16:39:38Z
From: Dirk Eddelbuettel
Subject: Regular expressions & sub

Bernd Weiss <bernd.weiss <at> uni-koeln.de> writes:
> I am struggling with the use of regular expression. I got
> 
> > as.character(test$sample.id)
>  [1] "1.11"   "10.11"  "11.11"  "113.31" "114.2"  "114.3"  "114.8"  
> 
> and need
> 
> [1] "11"   "11"  "11"  "31" "2"  "3"  "8"
> 
> I.e. remove everything before the "." .

Define the dot as the hard separator, and allow for multiple digits before it:

> sample.id <- c("1.11", "10.11", "11.11", "113.31", "114.2", "114.3", "114.8")
> gsub("^[0-9]*\.", "", sample.id)
[1] "11" "11" "11" "31" "2"  "3"  "8" 

Hope this helps,  Dirk