[R] strsplit question

Gabor Grothendieck ggrothendieck at gmail.com
Wed Oct 12 14:10:21 CEST 2011


On Wed, Oct 12, 2011 at 1:20 AM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:
> Dear R People:
>
> I have the following set of data
>> Block[1:5]
> [1] "5600-5699" "6100-6199" "9700-9799" "9400-9499" "8300-8399"
>
> and I want to split at the -
>
>> strsplit(Block[1:5],"-")
> [[1]]
> [1] "5600" "5699"
>
> [[2]]
> [1] "6100" "6199"
>
> [[3]]
> [1] "9700" "9799"
>
> [[4]]
> [1] "9400" "9499"
>
> [[5]]
> [1] "8300" "8399"
>

Try this:

> x <- c("5600-5699", "6100-6199", "9700-9799", "9400-9499", "8300-8399")
> sub("-.*", "", x) # before dash
[1] "5600" "6100" "9700" "9400" "8300"
> sub(".*-", "", x) # after dash
[1] "5699" "6199" "9799" "9499" "8399"

and here is another approach:

> library(gsubfn)
> m <- strapply(x, "\\d+", c, simplify = TRUE)
> m
     [,1]   [,2]   [,3]   [,4]   [,5]
[1,] "5600" "6100" "9700" "9400" "8300"
[2,] "5699" "6199" "9799" "9499" "8399"

Now m[1, ] and m[2, ] are the vectors of digits before and after the
dash.  Note that c in the strapply call can be replaced with
as.numeric if you want a numeric matrix instead.

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list