-Dealing with String
Handling, cleaning and processing character strings is becoming a prerequisite in daily data analysis.
#Character string basics
Creating Strings:
x <- "Berlin"
y <- "Paris" #The paste() function is expert for creating text
# paste together string a & b
paste(a, b)
## [1] "learning to create character strings"
# paste character and number strings
# (converts numbers to character class)
paste("The life of", pi)
## [1] "The life of 3.14159265358979"
# paste multiple strings
paste("I", "love", "R")
## [1] "I love R"
# paste multiple strings with a separating character
paste("I", "love", "R", sep = "-")
## [1] "I-love-R"
# use paste0() to paste without spaces btwn characters
paste0("I", "love", "R")
## [1] "IloveR"
# paste objects with different lengths
paste("R", 1:5, sep = " v1.")
## [1] "R v1.1" "R v1.2" "R v1.3" "R v1.4" "R v1.5"Converting to Strings:
Printing Strings:
Counting string elements and characters:
#String manipulation with base R
Case conversion:
Simple Character Replacement:
String Abbreviations:
Extract/Replace Substrings:
#String manipulation with stringr
Basic Operations:
Duplicate Characters within a String:
Remove Leading and Trailing Whitespace:
Pad a String with Whitespace:
#Set operatons for character strings
Set Union:
Set Intersection:
Identifying Different Elements:
Testing for Element Equality:
Testing for Exact Equality:
Identifying if Elements are Contained in a String:
Sorting a String:
Last updated