Desktop Survival Guide
by Graham Williams

Splitting Strings

strsplit is used to split a string into substrings:

> unlist(strsplit("abc def ghi jkl", " "))
[1] "abc" "def" "ghi" "jkl"

> unlist(strsplit("abc,def:ghi.jkl", "\\.|,|:"))
[1] "abc" "def" "ghi" "jkl"

The split pattern is a regular expression (hence the \\. is required to quote the full stop). For details on regular expressions see ?regexp

An example using the gsubfn package:

> library(gsubfn)
> s <- "AJKLOW(P)LKU(Y)OP"
> strapply(s, ".[(].[)]|.")[[1]]
 [1] "A"    "J"    "K"    "L"    "O"    "W(P)" "L"    "K"    "U(Y)" "O"   
[11] "P"

Copyright © Togaware Pty Ltd
Support further development through the purchase of the PDF version of the book.
The PDF version is a formatted comprehensive draft book (with over 800 pages).
Brought to you by Togaware. This page generated: Sunday, 22 August 2010