Learn to manipulate strings in R using the built in R functions. This tutorial is part of the Working With Data module of the R Programming Course offered by r-squared.
3. r-squared
Slide 3
Working With Data
www.r-squared.in/rprogramming
✓ Data Types
✓ Data Structures
✓ Data Creation
✓ Data Info
✓ Data Subsetting
✓ Comparing R Objects
✓ Importing Data
✓ Exporting Data
✓ Data Transformation
✓ Numeric Functions
✓ String Functions
✓ Mathematical Functions
4. r-squared
In this unit, we will explore string manipulation in R using the following functions:
Slide 4
String Functions
www.r-squared.in/rprogramming
● match()
● char.expand()
● grep()
● grepl()
● sub()
● substr()
● substring()
● strsplit()
● strtrim()
● chartr()
● tolower()
● toupper()
● toString()
● nchar()
● nzchar()
● noquote()
● pmatch()
● charmatch()
16. r-squared
Slide 16
noquote()
www.r-squared.in/rprogramming
Examples
> # example 1
> letters
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v"
[23] "w" "x" "y" "z"
> nql <- noquote(letters)
> nql
[1] a b c d e f g h i j k l m n o p q r s t u v w x y z
25. r-squared
Slide 25
grep()
www.r-squared.in/rprogramming
Description
grep() takes two arguments and seeks match for the first argument in the elements of its
second argument and returns the index of the elements that match.
Syntax
grep(pattern, character vector)
Returns
Integer vector indicating the index of elements in the character vector that match the
pattern
Documentation
help(grep)
29. r-squared
Slide 29
sub()
www.r-squared.in/rprogramming
Description
sub() takes three arguments and seeks match for the first argument in the elements of its
third argument and replaces them with the second argument.
Syntax
sub(pattern, replacement, character vector)
Returns
Character vector with replaced string
Documentation
help(sub)
35. r-squared
Slide 35
strsplit()
www.r-squared.in/rprogramming
Description
strsplit() takes two arguments and splits the elements of the first argument at the index
position of the second argument if the characters in the first argument match the secon
argument.
Syntax
strsplit(character vector, split)
Returns
List of elements in the first argument split by the second argument.
Documentation
help(strsplit)
40. r-squared
In the next unit, we will explore the following mathematical functions:
Slide 40
Next Steps...
www.r-squared.in/rprogramming
● abs()
● round()
● ceiling()
● floor()
● trunc()
● signif()
● jitter()
● format()
● formatC()
41. r-squared
Slide 41
Connect With Us
www.r-squared.in/rprogramming
Visit r-squared for tutorials
on:
● R Programming
● Business Analytics
● Data Visualization
● Web Applications
● Package Development
● Git & GitHub