Search notes:
R function: strsplit
strsplit( "Foo,Bar,Baz", ",")
# [[1]]
# [1] "Foo" "Bar" "Baz"
strsplit(c("Foo,Bar,Baz", "One,Two,Three"), ",")
# [[1]]
# [1] "Foo" "Bar" "Baz"
#
# [[2]]
# [1] "One" "Two" "Three"
strsplit( "Foo,Bar,Baz", ",")[[1]][2]
# [1] "Bar"
Returning a list
splits <- strsplit('a list of words', ' ')
typeof(splits)
#
# [1] "list"
The reason that strsplit
returns a list is that a vector of strings can be passed as first argument and still be distinguished in the returned list:
splits <- strsplit(c('foo bar baz', 'one two three four'), ' ')
length(splits)
#
# 2
splits[[2]][3]
#
# [1] "three"
Regular expression patterns
Unless the parameter
fixed
is set to
TRUE
(the default being
FALSE
) , the second parameter is a
regular expression which is used to split the string:
strsplit('42abc99def12345ghi', r'{[[:alpha:]]+}')
#
# [1] "42" "99" "12345"
See also
paste
concatentes strings to single string.