Skip to content

Instantly share code, notes, and snippets.

@toyeiei
Last active February 4, 2019 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toyeiei/4f244d23e7767a6fea8a31ba7ac5602e to your computer and use it in GitHub Desktop.
Save toyeiei/4f244d23e7767a6fea8a31ba7ac5602e to your computer and use it in GitHub Desktop.
# intro to stringr
install.packages("stringr")
library(stringr)
# check state.name
print(state.name)
# most used function in stringr
str_view(state.name, pattern = "New", match = T)
# create an example string
text <- "DataRockie is the coolest DS school on the internet, founded in Y2015.
It offers free online courses on datarockie.com, go check it!"
# basic regular expressions
str_view(text, "DataRockie")
str_view(text, "2015")
str_view(text, "datarockie")
# this character | means OR
# look for words 'DataRockie' OR 'datarockie'
str_view_all(text, "(D|d)ata(R|r)ockie")
# look for digits
str_view(text, "[0-9]")
str_view(text, "[0-9]+")
str_view(text, "\\d")
str_view(text, "\\d+")
# look for English alphabet
str_view_all(text, "[a-z]+")
str_view_all(text, "[A-Z]+")
# look for space
str_view_all(text, "\\s")
# split text by " "
text_split <- str_split(text, pattern = " ")[[1]]
text_split
# look for words start with 'i'
str_view_all(text_split, "^i.+")
# look for words end with 's'
str_view_all(text_split, ".+s$")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment