DATA MINING
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" |
\\.
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" |