Message-ID: <EDD48787-E077-42D8-A864-32269A3A2167@me.com>
Date: 2014-08-01T15:06:02Z
From: Marc Schwartz
Subject: Better use with gsub
In-Reply-To: <B08B6AF0CF8CA44F81B9983EEBDCD68699B11D5D@DC1VEX10MB001.air.org>
On Aug 1, 2014, at 9:46 AM, Doran, Harold <HDoran at air.org> wrote:
> I have done an embarrassingly bad job using a mixture of gsub and strsplit to solve a problem. Below is sample code showing what I have to start with (the vector xx) and I want to end up with two vectors x and y that contain only the digits found in xx.
>
> Any regex users with advice most welcome
>
> Harold
>
> xx <- c("S24:57", "S24:86", "S24:119", "S24:129", "S24:138", "S24:163")
> yy <- gsub("S","\\1", xx)
> a1 <- gsub(":"," ", yy)
> a2 <- sapply(a1, function(x) strsplit(x, ' '))
> x <- as.numeric(sapply(a2, function(x) x[1]))
> y <- as.numeric(sapply(a2, function(x) x[2]))
If a matrix is a satisfactory result, rather than two separate vectors:
> sapply(strsplit(gsub("S", "", xx), xx, split = ":"), as.numeric)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 24 24 24 24 24 24
[2,] 57 86 119 129 138 163
Regards,
Marc Schwartz