A consistent, simple and easy to use set of wrappers around the fantastic 'stringi' package. All function and argument names (and positions) are consistent, all functions deal with "NA"'s and zero length vectors in the same way, and the output from one function is easy to feed into the input of another.
Strings are not glamorous, high-profile components of R, but they do play a big role in many data cleaning and preparations tasks. R provides a solid set of string operations, but because they have grown organically over time, they can be inconsistent and a little hard to learn. Additionally, they lag behind the string operations in other programming languages, so that some things that are easy to do in languages like Ruby or Python are rather hard to do in R.
The stringr package aims to remedy these problems by providing a clean, modern interface to common string operations. More concretely, stringr:
Uses consistent functions and argument names.
Simplifies string operations by eliminating options that you don't need 95% of the time.
Produces outputs than can easily be used as inputs. This includes ensuring that missing inputs result in missing outputs, and zero length inputs result in zero length outputs.
To get the current released version from CRAN:
To get the current development version from github:
stringr provides the pipe,
%>%, from magrittr to make it easy to string together sequences of string operations:
letters %>%str_pad(5, "right") %>%str_c(letters)
Add sample datasets:
coll() now throw an error if you use them with
anything other than a plain string (#60). I've clarified that the replacement
boundary() has improved
defaults when splitting on non-word boundaries (#58, @lmullen).
str_detect() now can detect boundaries (by checking for a
str_count() > 0)
str_subset() works similarly.
str_extract_all() now work with
boundary(). This is
particularly useful if you want to extract logical constructs like words
str_extract_all() respects the
when used with
str_subset() now respects custom options for
str_replace_all() now behave correctly when a
replacement string contains
\\\\1, etc. (#83, #99).
str_split() gains a
simplify argument to match
str_view_all() create HTML widgets that display regular
expression matches (#96).
NA for indexes greater than number of words (#112).
stringr is now powered by stringi instead of base R regular expressions. This improves unicode and support, and makes most operations considerably faster. If you find stringr inadequate for your string processing needs, I highly recommend looking at stringi in more detail.
stringr gains a vignette, currently a straight forward update of the article that appeared in the R Journal.
str_c() now returns a zero length vector if any of its inputs are
zero length vectors. This is consistent with all other functions, and
standard R recycling rules. Similarly, using
str_c("x", NA) now
NA. If you want
str_replace_na() on the inputs.
str_replace_all() gains a convenient syntax for applying multiple pairs of
pattern and replacement to the same vector:
input <- c("abc", "def")str_replace_all(input, c("[ad]" = "!", "[cf]" = "?"))
str_match() now returns NA if an optional group doesn't match
(previously it returned ""). This is more consistent with
and other match failures.
str_subset() keeps values that match a pattern. It's a convenient
x[str_detect(x)] (#21, @jiho).
str_sort() allow you to sort and order strings
in a specified locale.
str_conv() to convert strings from specified encoding to UTF-8.
boundary() allows you to count, locate and split by
character, word, line and sentence boundaries.
The documentation got a lot of love, and very similar functions (e.g. first and all variants) are now documented together. This should hopefully make it easier to locate the function you need.
ignore.case(x) has been deprecated in favour of
fixed|regex|coll(x, ignore.case = TRUE),
perl(x) has been deprecated in
str_join() is deprecated, please use
fixed path in
str_wrap example so works for more R installations.
remove dependency on plyr
Zero input to
str_split_fixed returns 0 row matrix with
perl that switches to Perl regular expressions
str_match now uses new base function
regmatches to extract matches -
this should hopefully be faster than my previous pure R algorithm
str_wrap function which gives
strwrap output in a more convenient
word function extract words from a string given user defined
separator (thanks to suggestion by David Cooper)
str_locate now returns consistent type when matching empty string (thanks
to Stavros Macrakis)
str_count counts number of matches in a string.
str_trim receive performance tweaks - for large vectors this
should give at least a two order of magnitude speed up
str_length returns NA for invalid multibyte strings
fix small bug in internal