Dear all, 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 "." . TIA, Bernd
> x <- scan("clipboard", what="")Read 7 items > x [1] "1.11" "10.11" "11.11" "113.31" "114.2" "114.3" "114.8" > gsub("[0-9]*\\.", "", x) [1] "11" "11" "11" "31" "2" "3" "8" > Bernd Weiss wrote:> Dear all, > > 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 "." . > > TIA, > > Bernd > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
One solution is test <- c("1.11","10.11","11.11","113.31","114.2","114.3") id <- unlist(lapply(strsplit(test,"[.]"),function(x) {x[2]}))> -----Original Message----- > From: Bernd Weiss [mailto:bernd.weiss at uni-koeln.de] > Sent: Thursday, August 18, 2005 12:10 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Regular expressions & sub > > > Dear all, > > 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 "." . > > TIA, > > Bernd > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
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
On 18 Aug 2005 at 21:17, Peter Dalgaard wrote:> Dirk Eddelbuettel <edd at debian.org> writes: > > > 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" > > Or, more longwinded, but with less assumptions about what goes before > the dot: > > > gsub("^.*\\.(.*)$","\\1",sample.id) > [1] "11" "11" "11" "31" "2" "3" "8"Wow, thanks a lot for all the valuable suggestions. Bernd