Working With Text Data in R
Working With Text Data in R
str_view_all(suits, "[ae]")
Clubs
# Substitute value in a string with an expression
Diamonds
Learn R online at www.DataCamp.com glue('The answer is {ans}', ans = 30 + 10) # The answer is 40
Hearts
Spades
# Substitute value in a string with an expression
> Packages to install for this cheat sheet cards %>% glue_data("{value} of {suit}")
str_which(suits, "[ae]") # 2 3 4
# 8 of Diamonds
# Queen of Hearts
Some functionality from this cheat sheet comes with base-R, but the following packages are also used # Count the number of matches with str_count()
str_count(suits, "[ae]") # 0 1 2 2
library(stringr)
# Wrap strings across multiple lines
# Locate the position of matches within strings with str_locate()
library(snakecase)
str_wrap('The answer to the universe is 42', width = 25)
str_locate(suits, "[ae]")
# universe is 42 # [1,] NA NA
Functions with names starting str_ are from stringr; those with names starting to_ are from snakecase;
those with glue in the name are from glue. # [2,] 3 3
# [3,] 2 2
# [4,] 3 3
Throughout this cheat sheet, we’ll be using this vector containing the following strings. str_split(suits, pattern = "")
str_match(suits, ".([ae])(.)")
nchar(suits) # Returns 5 8 6 6
# "Clubs"
# "He" "rts"
# "Sp" "des"
# [,1] [,2]
str_pad(suits, width = 8) # Returns " Clubs" "Diamonds" " Hearts" " Spades"
Learn R Online at
# Collapse character vector to string with paste() or paste0()
www.DataCamp.com
# Convert to title case with to_title_case()