-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.R
36 lines (36 loc) · 1.09 KB
/
string.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
strsplit0 <- function(strVec, split, simplify = TRUE) {
# Split a string vector with multiple split
#
# Args:
# strVec: One vector whose elements are to be split.
# split: character vector containing regular expression(s) to use for splitting.
# simplify: If TRUE, returns character vector; if not, list; Default is TRUE.
#
# Returns:
# vector or list of split characters.
if (simplify == TRUE) {
strsplit0 <- c()
for (sp in split) {
strVec <- strVecSplit(strVec, sp)
}
strsplit0 <- strVec
return(strsplit0)
} else {
strsplit0 <- list()
for (str in strVec) {
for (sp in split) {
str <- strVecSplit(str, sp)
}
strsplit0[[length(strsplit0) + 1]] <- str
}
return(strsplit0)
}
}
strVecSplit <- function(strVec, split) {
strVecSplit <- c()
for (str in strVec) {
unlist <- unlist(strsplit(str, split = split))
strVecSplit <- c(strVecSplit, unlist)
}
return(strVecSplit)
} # Used by strsplit0()