This list is a collection of specific commands to do specific text/string manipulations in R.

Insert a blank between 2 bits of text.

This uses regex capturing groups.

gsub("(#)(\\w)","\\1 \\2", text) # first symbol is a #, second is a word character. an empty space is inserted.  
gsub("(#)([A-Z])","\\1 \\2", text) # first symbol is a #, second is an upper case letter. an empty space is inserted.

Replace text in a column of a dataframe

This example replaces the text bit NA (not the symbol that stands for missing) with the text “present” in a column called “Dates”.

dat %>% mutate( Dates = stringr::str_replace(Dates, "NA","present") ) %>%