stringr in R
Consistent, pipe-friendly string functions.
library(stringr)
names <- c("Ada Lovelace", "Grace Hopper")
lengths <- str_length(names)
upper <- str_to_upper(names)
titled <- str_to_title("ada lovelace")
has_grace <- str_detect(names, "Grace")
surnames <- str_extract(names, "\\w+$")
swapped <- str_replace(names, "Ada", "A.")
parts <- str_split(names, " ")
padded <- str_pad(names, 20, side = "right")
trimmed <- str_trim(" spaced ")
How it works
- Every function starts
str_and takes the string first. str_detect,str_extract,str_replaceuse patterns.str_padandstr_trimhandle whitespace.
Keywords and builtins used here
clibrarystr_detectstr_extractstr_lengthstr_padstr_replacestr_splitstr_to_titlestr_to_upperstr_trim
The run, in numbers
- Lines
- 14
- Characters to type
- 392
- Tokens
- 92
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 55 seconds.
Step 1 of 3 in stringr, step 5 of 10 in Functional & text tools.