[R] strsplit question

David Winsemius dwinsemius at comcast.net
Wed Oct 12 08:27:48 CEST 2011


On Oct 12, 2011, at 1:20 AM, Erin Hodgess 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"
>
>>
>
> What is the best way to extract the pieces that are to the left of the
> dash, please?
>

 > sub("\\-.*$", "", c("5600-5699", "6100-6199", "9700-9799",  
"9400-9499", "8300-8399") )
[1] "5600" "6100" "9700" "9400" "8300"

-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list