[R] strsplit and regexp

Gabor Grothendieck ggrothendieck at gmail.com
Sat Aug 30 19:03:42 CEST 2008


Try this:

> library(gsubfn)
> strapply(x, "[0-9]+| +[^ ]+ *")[[1]]
[1] "2"       " Value " "34"      " a-c "   "45"      " t"

which keeps the spaces around the non-numeric entries as in
your example; however, if keeping the spaces is not important
then its even easier:

> strsplit(x, " ")[[1]]
[1] "2"     "Value" "34"    "a-c"   "45"    "t"

On Sat, Aug 30, 2008 at 12:51 PM, Patrick Hausmann
<Patrick.Hausmann at uni-bremen.de> wrote:
> Dear list,
>
> I am trying to split a string using regexp:
>
> x <- "2 Value 34 a-c 45 t"
> strsplit(x, "[0-9]")
>
> [[1]]
> [1] ""  " Value "  ""  " a-c "  ""  " t"
>
> But I don't want to lose the digits (pattern), the result
> should be:
>
> [[1]]
> [1] "2" " Value "  "34"  " a-c "  "45" " t"
>
> Thanks for any tipp
> Patrick
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list