[R] Is there a better way to parse strings than this?

Hadley Wickham hadley at rice.edu
Wed Apr 13 14:48:35 CEST 2011


On Wed, Apr 13, 2011 at 5:18 AM, Dennis Murphy <djmuser at gmail.com> wrote:
> Hi:
>
> Here's one approach:
>
> strings <- c(
> "A5.Brands.bought...Dulux",
> "A5.Brands.bought...Haymes",
> "A5.Brands.bought...Solver",
> "A5.Brands.bought...Taubmans.or.Bristol",
> "A5.Brands.bought...Wattyl",
> "A5.Brands.bought...Other")
>
> slist <- strsplit(strings, '\\.\\.\\.')

Or with stringr:

library(stringr)
str_split_fixed(strings, fixed("..."), n = 2)

# or maybe
str_match(strings, "(..).*\\.\\.\\.(.*)")

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



More information about the R-help mailing list