0% found this document useful (0 votes)
4 views47 pages

Module 4 R

Module 4 covers data manipulation techniques using the dplyr package in R, including functions such as filter(), distinct(), arrange(), select(), rename(), mutate(), transmute(), and summarize(). It also introduces the apply() family of functions (apply(), lapply(), sapply(), tapply(), and aggregate()) for applying functions to data structures. Additionally, the module discusses the Plyr package for data manipulation, focusing on the ddply() and ldply() functions for splitting and combining data.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views47 pages

Module 4 R

Module 4 covers data manipulation techniques using the dplyr package in R, including functions such as filter(), distinct(), arrange(), select(), rename(), mutate(), transmute(), and summarize(). It also introduces the apply() family of functions (apply(), lapply(), sapply(), tapply(), and aggregate()) for applying functions to data structures. Additionally, the module discusses the Plyr package for data manipulation, focusing on the ddply() and ldply() functions for splitting and combining data.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MODULE-4 (DATA MANIPULATION)

Data Manipulation

Data manipulation involves modifying data to make it easier to read and to be more
organized. We manipulate data for analysis and visualization. It is also used with the term
‘data exploration’ which involves organizing data using available sets of variables.
At times, the data collection process done by machines involves a lot of errors and
inaccuracies in reading. Data manipulation is also used to remove these inaccuracies and
make data more accurate and precise.
So to use the data manipulation function, first need to import the dplyr package
using library(dplyr) line of code. Below is the list of a few data manipulation functions
present in dplyr package.

Function Name Description

filter() Produces a subset of a Data Frame.

distinct() Removes duplicate rows in a Data Frame

arrange() Reorder the rows of a Data Frame

select() Produces data in required columns of a Data Frame

rename() Renames the variable names

mutate() Creates new variables without dropping old ones.

transmute() Creates new variables by dropping the old.

summarize() Gives summarized data like Average, Sum, etc.

filter() method
The filter() function is used to produce the subset of the data that satisfies the condition
specified in the filter() method. In the condition, we can use conditional operators, logical
operators, NA values, range operators etc. to filter out data. Syntax of filter() function is
given below-
filter(dataframeName, condition)
Example:
In the below code we used filter() function to fetch the data of players who scored more
than 100 runs from the “stats” data frame.

# import dplyr package


library(dplyr)

# create a data frame


stats <- [Link](player=c('A', 'B', 'C', 'D'),
runs=c(100, 200, 408, 19),
wickets=c(17, 20, NA, 5))

# fetch players who scored more


# than 100 runs
filter(stats, runs>100)

Output
player runs wickets
1 B 200 20
2 C 408 NA
distinct() method
The distinct() method removes duplicate rows from data frame or based on the specified
columns. The syntax of distinct() method is given below-
distinct(dataframeName, col1, col2,.., .keep_all=TRUE)
Example:
Here in this example, we used distinct() method to remove the duplicate rows from the data
frame and also remove duplicates based on a specified column.

# import dplyr package


library(dplyr)

# create a data frame


stats <- [Link](player=c('A', 'B', 'C', 'D', 'A', 'A'),
runs=c(100, 200, 408, 19, 56, 100),
wickets=c(17, 20, NA, 5, 2, 17))
# removes duplicate rows
distinct(stats)
#remove duplicates based on a column
distinct(stats, player, .keep_all = TRUE)

Output
player runs wickets
1 A 100 17
2 B 200 20
3 C 408 NA
4 D 19 5
5 A 56 2
player runs wickets
1 A 100 17
2 B 200 20
3 C 408 NA
4 D 19 5
arrange() method
In R, the arrange() method is used to order the rows based on a specified column. The
syntax of arrange() method is specified below-
arrange(dataframeName, columnName)
Example:
In the below code we ordered the data based on the runs from low to high using arrange()
function.

# import dplyr package


library(dplyr)

# create a data frame


stats <- [Link](player=c('A', 'B', 'C', 'D'),
runs=c(100, 200, 408, 19),
wickets=c(17, 20, NA, 5))
# ordered data based on runs
arrange(stats, runs)

Output
player runs wickets
1 D 19 5
2 A 100 17
3 B 200 20
4 C 408 NA
select() method
The select() method is used to extract the required columns as a table by specifying the
required column names in select() method. The syntax of select() method is mentioned
below-
select(dataframeName, col1,col2,…)
Example:
Here in the below code we fetched the player, wickets column data only using select()
method.
# import dplyr package
library(dplyr)

# create a data frame


stats <- [Link](player=c('A', 'B', 'C', 'D'),
runs=c(100, 200, 408, 19),
wickets=c(17, 20, NA, 5))
# fetch required column data
select(stats, player, wickets)

Output
player wickets
1 A 17
2 B 20
3 C NA
4 D 5
rename() method
The rename() function is used to change the column names. This can be done by the below
syntax-
rename(dataframeName, newName=oldName)
Example:
In this example, we change the column name “runs” to “runs_scored” in stats data
frame.

# import dplyr package


library(dplyr)

# create a data frame


stats <- [Link](player=c('A', 'B', 'C', 'D'),
runs=c(100, 200, 408, 19),
wickets=c(17, 20, NA, 5))
# renaming the column
rename(stats, runs_scored=runs)

Output
player runs_scored wickets
1 A 100 17
2 B 200 20
3 C 408 NA
4 D 19 5
mutate() & transmute() methods
These methods are used to create new variables. The mutate() function creates new
variables without dropping the old ones but transmute() function drops the old variables
and creates new variables. The syntax of both methods is mentioned below-
mutate(dataframeName, newVariable=formula)
transmute(dataframeName, newVariable=formula)
Example:
In this example, we created a new column avg using mutate() and transmute() methods.

# import dplyr package


library(dplyr)

# create a data frame


stats <- [Link](player=c('A', 'B', 'C', 'D'),
runs=c(100, 200, 408, 19),
wickets=c(17, 20, 7, 5))

# add new column avg


mutate(stats, avg=runs/4)

# drop all and create a new column


transmute(stats, avg=runs/4)

Output
player runs wickets avg
1 A 100 17 25.00
2 B 200 20 50.00
3 C 408 7 102.00
4 D 19 5 4.75
avg
1 25.00
2 50.00
3 102.00
4 4.75
Here mutate() functions adds a new column for the existing data frame without dropping
the old ones where as transmute() function created a new variable but dropped all the old
columns.
summarize() method
Using the summarize method we can summarize the data in the data frame by using
aggregate functions like sum(), mean(), etc. The syntax of summarize() method is specified
below-
summarize(dataframeName, aggregate_function(columnName))
Example:
In the below code we presented the summarized data present in the runs column using
summarize() method.

# import dplyr package


library(dplyr)

# create a data frame


stats <- [Link](player=c('A', 'B', 'C', 'D'),
runs=c(100, 200, 408, 19),
wickets=c(17, 20, 7, 5))

# summarize method
summarize(stats, sum(runs), mean(runs))

Output
sum(runs) mean(runs)
1 727 181.75

The apply() collection is a part of R essential package. This family of functions helps us to
apply a certain function to a certain data frame, list, or vector and return the result as a list
or vector depending on the function we use. There are these following four types of
function in apply() function family:
apply() function
The apply() function lets us apply a function to the rows or columns of a matrix or data
frame. This function takes matrix or data frame as an argument along with function and
whether it has to be applied by row or column and returns the result in the form of a vector
or array or list of values obtained.
Syntax: apply( x, margin, function )
Parameters:
 x: determines the input array including matrix.
 margin: If the margin is 1 function is applied across row, if the margin is 2 it is
applied across the column.
 function: determines the function that is to be applied on input data.

Example:
Here, is a basic example showcasing the use of apply() function along rows as well as
columns.

# create sample data


sample_matrix <- matrix(C<-(1:10),nrow=3, ncol=10)

print( "sample matrix:")


sample_matrix

# Use apply() function across row to find sum


print("sum across rows:")
apply( sample_matrix, 1, sum)
# use apply() function across column to find mean
print("mean across columns:")
apply( sample_matrix, 2, mean)

Output:

lapply() function
The lapply() function helps us in applying functions on list objects and returns a list object
of the same length. The lapply() function in the R Language takes a list, vector, or data
frame as input and gives output in the form of a list object. Since the lapply() function
applies a certain operation to all the elements of the list it doesn’t need a MARGIN.
Syntax: lapply( x, fun )
Parameters:
 x: determines the input vector or an object.
 fun: determines the function that is to be applied to input data.

Example:
Here, is a basic example showcasing the use of the lapply() function to a vector.

# create sample data


names <- c("priyank", "abhiraj","pawananjani",
"sudhanshu","devraj")
print( "original data:")
names

# apply lapply() function


print("data after lapply():")
lapply(names, toupper)

Output:
sapply() function
The sapply() function helps us in applying functions on a list, vector, or data frame and
returns an array or matrix object of the same length. The sapply() function in the R
Language takes a list, vector, or data frame as input and gives output in the form of an array
or matrix object. Since the sapply() function applies a certain operation to all the elements
of the object it doesn’t need a MARGIN. It is the same as lapply() with the only difference
being the type of return object.
Syntax: sapply( x, fun )
Parameters:
 x: determines the input vector or an object.
 fun: determines the function that is to be applied to input data.

Example:
Here, is a basic example showcasing the use of the sapply() function to a vector.

# create sample data


sample_data<- [Link]( x=c(1,2,3,4,5,6),
y=c(3,2,4,2,34,5))
print( "original data:")
sample_data

# apply sapply() function


print("data after sapply():")
sapply(sample_data, max)

Output:
tapply() function
The tapply() helps us to compute statistical measures (mean, median, min, max, etc..) or a
self-written function operation for each factor variable in a vector. It helps us to create a
subset of a vector and then apply some functions to each of the subsets. For example, in an
organization, if we have data of salary of employees and we want to find the mean salary
for male and female, then we can use tapply() function with male and female as factor
variable gender.
Syntax: tapply( x, index, fun )
Parameters:
 x: determines the input vector or an object.
 index: determines the factor vector that helps us distinguish the data.
 fun: determines the function that is to be applied to input data.

Example:
Here, is a basic example showcasing the use of the tapply() function on the diamonds
dataset which is provided by the tidyverse package library.

# load library tidyverse


library(tidyverse)

# print head of diamonds dataset


print(" Head of data:")
head(diamonds)

# apply tapply function to get average price by cut


print("Average price for each cut of diamond:")
tapply(diamonds$price, diamonds$cut, mean)

Output:
Aggregate() function is used to get the summary statistics of the data by group. The
statistics include mean, min, sum. max etc.
Syntax:
aggregate(dataframe$aggregate_column, list(dataframe$group_column), FUN)
where
 dataframe is the input dataframe.
 aggregate_column is the column to be aggregated in the dataframe.
 group_column is the column to be grouped with FUN.
 FUN represents sum/mean/min/ max.

Example : R program to create with 4 columns and group with subjects and get the average
(mean).

# create a dataframe with 4 columns


data = [Link](subjects=c("java", "python", "java",
"java", "php", "php"),
id=c(1, 2, 3, 4, 5, 6),
names=c("manoj", "sai", "mounika",
"durga", "deepika", "roshan"),
marks=c(89, 89, 76, 89, 90, 67))

# display
print(data)

# aggregate average of marks with subjects


print(aggregate(data$marks, list(data$subjects), FUN=mean))

Output:
What is Plyr Package?

Plyr is a package for data manipulation in R that provides a set of functions for splitting,
applying, and combining data. It is based on the concept of split-apply-combine, where a
dataset is first split into smaller subsets, a function is applied to each subset, and the results
are then combined into a single output. This process is useful for tasks such as aggregating
data, summarizing data, and transforming data.

Installing and Loading Plyr Package:

Before using the plyr package, it needs to be installed and loaded into R. The package can
be installed using the following command:

[Link]("plyr")

After the package is installed, it can be loaded into R using the following command:

library(plyr)

1. Splitting Data using ddply( ) functions:

The ddply( ) function is a powerful tool for splitting data frames into smaller subsets,
applying a function to each subset, and then combining the results into a new data frame.
The name “ddply” stands for “split, apply, and combine”, which summarizes the three main
steps of the function. Here are the main arguments of ddply():
Syntax:
Parameters: `data`
object:The input data frame that you want to split and process.
Syntax:
Parameters: `variables`
object:One or more grouping variables that define how the data should be split.
Syntax:
Parameters: `fun`
object:A function that you want to apply to each subset of the data frame.
Syntax:
Parameters: `…`
object:Additional arguments that are passed to the function specified in [Link]’s an
example of how to use ddply() to calculate the mean miles per gallon (mpg) of cars in the
mtcars dataset, grouped by the number of cylinders in the engine:

library(plyr)
# Using ddply to group by number of cylinders and calculate mean mpg
ddply(mtcars, .(cyl), summarise, mean_mpg = mean(mpg))
In this example, ddply() is used to group the mtcars dataset by the cyl variable (number of
cylinders), and then the summarise() function is used to calculate the mean mpg for each
group. The resulting output is a data frame with two columns: cyl and mean_mpg.

2. Combining the results using ldply( ) function:

The ldply() function is used to convert a list of data frames or vectors into a single data
frame, with each element of the list becoming a row of the output data frame. The name
“ldply” stands for “list and bind data frames”, which summarizes the main action of the
function. Finally, the ldply() function returns a data frame that contains all the elements of
the input list, stacked on top of each other. Here are the main arguments of ldply():
Syntax:
Parameters: `data`
object:The input list that you want to convert to a data frame.
Syntax:
Parameters: `.fun`
object:An optional function that you want to apply to each element of the list before
converting it to a data frame.
Syntax:
Parameters: `…`
object:Additional arguments that are passed to the function specified in .fun.
Example:

library(plyr)
# Create a list of data frames
countries_1 <- [Link](country = c("USA", "Canada", "Mexico"), population =
c(328, 37, 130))
countries_2 <- [Link](country = c("Brazil", "Argentina", "Chile"), population =
c(211, 45, 19))
countries_list <- list(countries_1, countries_2)
# Use ldply() to combine the list of data frames into a single data frame
combined_df <- ldply(countries_list, [Link])
# View the resulting data frame
combined_df

In this example, we first create a list of two data frames (countries_1 and countries_2) using
[Link]() function. Then, we combine these data frames into a list called countries_list.
Finally, we use ldply() function to combine all the data frames in countries_list into a single
data frame called combined_df. The resulting data frame contains information about all the
countries in the original data frames.

3. Combining Data using adply( ) function:


The adply() function is used to apply a function to each subset of a data frame and then
combines the results into a new data frame. The a in adply() stands for “array”, meaning
that it can be used with arrays of any dimensions. The arguments for adply() are:
Syntax:
Parameters: `data`
object:the input data frame or array.
Syntax:
Parameters: `margins`
object:the dimensions of the array to split over (in this example, we used 2 to split over the
second dimension)
Syntax:
Parameters: `FUN`
object:the function to apply to each subset of the array (in this example, we used an
anonymous function that calculates the mean and standard deviation of each column)
Syntax:
Parameters: `…`
object:additional arguments to pass to the function specified in FUN (if any)
Example:

library(plyr)
# Create a sample matrix
mat <- matrix(1:9, nrow = 3)
# Display created matrix
mat
# Use adply() to calculate the sum of each row
result <- adply(mat, 1, function(x) sum(x))
# View the result
Result

In this example, the adply() function is used to apply the sum() function to each row of the
matrix mat. The second argument (1) specifies that we want to apply the function to each
subset of the array consisting of one row and all columns. The third argument is an
anonymous function that calculates the sum of each row. The resulting result data frame has
one column and three rows (one for each row in mat). The values in each row correspond to
the sum of that row.

4. Join Two Data Frames using join( ) function:

join() is a function from the plyr package in R that is used to join two data frames by a
common column. The join() function takes several arguments, including:
Syntax:
Parameters: `x`, `y`
object: Data frames join.
Syntax:
Parameters: `by`
object: The column(s) to join the data frames .
Syntax:
Parameters: `type`
object: The type of join to perform (e.g. “inner”, “outer”, “left”, “right”).
Syntax:
Parameters: `suffix`
object:A character vector to append to overlapping variable names (defaults to c(“.x”,
“.y”))
Example:

library(plyr)
# Create two sample data frames
df1 <- [Link](
id = c(1, 2, 3),
name = c("Alice", "Bob", "Charlie")
)
df2 <- [Link](
id = c(2, 3, 4),
age = c(25, 30, 35)
)
# Print the created dataset
df1
df2
# Use join() to combine the data frames
result <- join(df1, df2, by = "id")

In this example, the join() function is used to combine two data frames (df1 and df2) based
on a common column (id). The by argument specifies the name of the common column.
The resulting result data frame has three columns (id, name, age) and two rows (one for
each matching value of id in df1 and df2). The values in the name and age columns
correspond to the names and ages of the individuals with the matching id value.

5. Summary Statistics using summarise( ) function:

The summarise() function in the plyr package of R is used to aggregate data and calculate
summary statistics by groups. The summarise() function takes several arguments,
including:
Syntax:
Parameters: `data`
object: The data frame to summarize.
Syntax:
Parameters: `…`
object: a list of expressions that calculate summary statistics (e.g. mean(value), sd(value),
etc.)
Example:

# Load the plyr package


library(plyr)
# Create a data frame with two columns: group and value
df <- [Link](group = c("A", "A", "B", "B", "B"), value = c(2, 4, 6, 8, 10))
# Summarize the data by group, calculating the
# mean and standard deviation of the value column
summary_df <- summarise(group_by(df, group), mean = mean(value), sd = sd(value))
# Print the summary data frame to the console
summary_df

Data Reshaping in R Programming

Data processing is done by taking data as input from a data frame where the data is organized
into rows and columns. Data frames are mostly used since extracting data is much simpler
and hence easier. But sometimes we need to reshape the format of the data frame from the
one we receive. Hence, in R, we can split, merge and reshape the data frame using various
functions.
The various forms of reshaping data in a data frame are:
 Transpose of a Matrix
 Joining Rows and Columns
 Merging of Data Frames
 Melting and Casting
Why R – Data Reshaping is Important?
While doing an analysis or using an analytic function, the resultant data obtained because of
the experiment or study is generally different. The obtained data usually has one or more
columns that correspond or identify a row followed by a number of columns that represent
the measured values. We can say that these columns that identify a row can be the composite
key of a column in a database.
Transpose of a Matrix
We can easily calculate the transpose of a matrix in R language with the help of the t()
function. The t() function takes a matrix or data frame as an input and gives the transpose of
that matrix or data frame as its output.
Syntax:
t(Matrix/ Data frame)
Example:

# R program to find the transpose of a matrix


first <- matrix(c(1:12), nrow=4, byrow=TRUE)
print("Original Matrix")
first
first <- t(first)
print("Transpose of the Matrix")
first

Output:
[1] "Original Matrix"
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12

[1] "Transpose of the Matrix"

[,1] [,2] [,3] [,4]


[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12

Joining Rows and Columns in Data Frame


In R, we can join two vectors or merge two data frames using functions. There are basically
two functions that perform these tasks:
cbind():
We can combine vectors, matrix or data frames by columns using cbind() function.
Syntax: cbind(x1, x2, x3)
where x1, x2 and x3 can be vectors or matrices or data frames.
rbind():
We can combine vectors, matrix or data frames by rows using rbind() function.
Syntax: rbind(x1, x2, x3)
where x1, x2 and x3 can be vectors or matrices or data frames.
Example:

# Cbind and Rbind function in R


name <- c("Shaoni", "esha", "soumitra", "soumi")
age <- c(24, 53, 62, 29)
address <- c("puducherry", "kolkata", "delhi", "bangalore")

# Cbind function
info <- cbind(name, age, address)
print("Combining vectors into data frame using cbind ")
print(info)
# creating new data frame
newd <- [Link](name=c("sounak", "bhabani"),
age=c("28", "87"),
address=c("bangalore", "kolkata"))
# Rbind function
[Link] <- rbind(info, newd)
print("Combining data frames using rbind ")
print([Link])

Output:
[1] "Combining vectors into data frame using cbind "
name age address
[1,] "Shaoni" "24" "puducherry"
[2,] "esha" "53" "kolkata"
[3,] "soumitra" "62" "delhi"
[4,] "soumi" "29" "bangalore"

[1] "Combining data frames using rbind "

name age address


1 Shaoni 24 puducherry
2 esha 53 kolkata
3 soumitra 62 delhi
4 soumi 29 bangalore
5 sounak 28 bangalore
6 bhabani 87 kolkata
Merging two Data Frames
In R, we can merge two data frames using the merge() function provided both the data
frames should have the same column names. We may merge the two data frames based on a
key value.
Syntax: merge(dfA, dfB, …)
Example:

# Merging two data frames in R


d1 <- [Link](name=c("shaoni", "soumi", "arjun"),
ID=c("111", "112", "113"))
d2 <- [Link](name=c("sounak", "esha"),
ID=c("114", "115"))
total <- merge(d1, d2, all=TRUE)
print(total)

Output:
name ID
1 arjun 113
2 shaoni 111
3 soumi 112
4 esha 115
5 sounak 114
Melting and Casting
Data reshaping involves many steps in order to obtain desired or required format. One of the
popular methods is melting the data which converts each row into a unique id-variable
combination and then casting it. The two functions used for this process:
melt():
It is used to convert a data frame into a molten data frame.
Syntax: melt(data, …, [Link]=FALSE, [Link]=”value”)
where,
data: data to be melted
… : arguments
[Link]: converts explicit missings into implicit missings
[Link]: storing values
dcast():
It is used to aggregate the molten data frame into a new form.
Syntax: melt(data, formula, [Link])
where,
data: data to be melted
formula: formula that defines how to cast
[Link]: used if there is a data aggregation
Example:

library(reshape2)

a <- [Link](id = c("1", "1", "2", "2"),


points = c("1", "2", "1", "2"),
x1 = c("5", "3", "6", "2"),
x2 = c("6", "5", "1", "4"))

# Convert numeric columns to actual numeric values


a$x1 <- [Link]([Link](a$x1))
a$x2 <- [Link]([Link](a$x2))
print("Melting")
m <- melt(a, id = c("id", "points"))
print(m)
print("Casting")
idmn <- dcast(m, id ~ variable, mean)
print(idmn)

Output:
[1] "Melting"

id points variable value


1 1 1 x1 5
2 1 2 x1 3
3 2 1 x1 6
4 2 2 x1 2
5 1 1 x2 6
6 1 2 x2 5
7 2 1 x2 1
8 2 2 x2 4

[1] "Casting"

id x1 x2
1 1 4 5.5
2 2 4 2.5

1 Melting and Casting


1.1 Why Reshape Your Data
Reshape2 is a package that allows us to easily transform our data into whatever structure we
may need. Many of us are used to seeing our data structured so that corresponds to a single
participant and each column corresponds to a variable. This type of data structure is known
as wide format. However, many of the packages in R require that we stretch our data so that
a single participant may occupy multiple rows. This type of data structure is known as long
format. For example, ggplot2 and some data analysis functions require long format. Any of
you who have tried to restructure their data using Excel or SPSS will immediately recognize
the immense power of this package. Therefore, without further ado, let’s get to it.

String Manipulation Functions in R

String Manipulations in R

In this section, we will discuss how to manipulate strings in the R programming language
with different types of in-built functions provided by R packages.

A brief summary of function names and their corresponding actions or performance is


described in the below table.

String Manipulations Functions Description

paste() To concatenate the strings.

format() To format the numerical values.


nchar() To count the characters in the string.

substr() To extract the specific characters from the string

grep() To extract a specific pattern from a string.

strsplit() To split the elements in a character string.

tolower() Translates lower to upper case strings

toupper() Translates upper to lower case strings

[Link] to concatenating string : paste() function in R

The paste () function in R is used to combine two strings or more than two strings together to
form a new string. In other words, the paste() function concatenates vectors after converting
them into characters.

The paste function converts the arguments present in it to character strings and they are
concatenated. If the arguments are vectors, they are concatenated term-by-term to give a
character vector result. Vector arguments are recycled as needed, with zero-length arguments
being recycled to " " only if recycle0 is not true or collapse is not NULL.

Syntax of paste() function in R

paste (..., sep = " ", collapse = NULL, recycle0 = FALSE)


paste0(..., collapse = NULL, recycle0 = FALSE)

Where,

 … is one or more R objects, to be converted to character vectors. The R objects can


be considered as string1, string2, string3, etc representing arguments to be combined.

 sep is a character string to separate the arguments which are optional. Not
NA_character.
 collapse is an optional character string to eliminate the space in between two strings.
Not NA_character. Not to eliminate the space between two words of a single string.

 recycle0 is logical indicating if zero-length character arguments should lead to the


zero-length character(0) after the sep-phase (which turns into " " in the collapse-
phase, i.e., when the collapse is not NULL).

 paste0(..., collapse) is equivalent to paste(..., sep = "", collapse), slightly more


efficiently.

If a value is specified for collapse, the values in the result are then concatenated into a single
string, with the elements being separated by the value of collapse.

Program using basic paste() function

#use paste() function


string1 = "Welcome"
string2 = "to"
string3 = "Learn etutorials" c string2, string3)
print(concatStr)

Output:

[1] "Welcome to Learn etutorials"

Program using paste() with sep

paste("Welcome", "to"," Learn etutorials ", sep = "----") #paste with separator

The sep=”---“ is a character string that separates arguments welcome, to, Learn etutorials
given inside a paste() function.

Output:

[1] "Welcome----to---- Learn etutorials "


When the separator is changed to sep=”___” the input and output becomes

> paste("Welcome", "to"," Learn etutorials ", sep = "___") #paste with separator
[1] "Welcome___to___ Learn etutorials "

Program using collapse in paste() function

x <- c("example","of","paste","with","collapse")
print(x)
paste(x)

The vector object x contains several different character strings. When the paste function is
applied over this vector object x, it returns the corresponding result the same as a vector
output. Let us see the output to differentiate vector output(print x) and when paste() function
is applied to vector x (paste(x))..

> print(x)
[1] "example" "of" "paste" "with" "collapse"
> paste(x)
[1] "example" "of" "paste" "with" "collapse"

You can observe that both results are the same. The character strings of vector object x are
not merged with the paste function. So in order to merge elements of a vector object you need
to specify the collapse argument.

paste(x,collapse = " ")

In the collapse argument, you need to specify another character string as a separator. Here
collapse = “ “ implies the vector object x elements is going to merge with a blank value.
When the code is run the output is

paste(x,collapse = " ")


[1] "example of paste with collapse"

Another character string is returned by merging all elements in the vector.

Difference between paste() and paste0()


paste() paste0()

INPUT paste("Welcome", "to"," paste0("Welcome", "to"," Learn


Learn etutorials ", sep = etutorials ")
"")

OUTPUT [1] "Welcome to Learn [1] "Welcome to Learn etutorials "


etutorials "
INFERENCE Need to specify a separator No need to specify a separator. By
ie sep. default uses an empty character string
as a separator.

Inference: paste0() function is an alternative provided in the R programming language instead


of the paste() function which is a more efficient and convenient function for merging strings.
From the above table, it is clear that both paste() and paste0() provide similar output.

[Link]() function in R

The nchar() function in the R programming language counts the number of characters
including spaces in a string. This function consists of a character vector as its arguments and
returns a vector whose elements comprising of different sizes of the elements of a string. The
nchar function in R is the fastest and most efficient way to find out if elements of a character
vector are non-empty strings or not.

Syntax of nchar() function in R

nchar(x, type = "chars", allowNA = FALSE, keepNA = NA)


nzchar(x, keepNA = FALSE)

Where Arguments

 x character vector, or a vector to be coerced to a character vector. Giving a factor is an


error.

 type character string: partial matching to one of c("bytes", "chars", "width").

 allowNA logical: should NA be returned for invalid multibyte strings or "bytes"-


encoded.

 keepNA logical: should NA be returned when x is NA? If false, nchar() returns 2, as


that is the number of printing characters used when strings are written to output, and
nzchar() is TRUE. The default for nchar(), NA, means to use keepNA = TRUE unless
type is "width".

Consider an example with a single character object or a variable string [Link] check how
many number characters are contained in string str0 we apply nchar() function over str0.
The function returns a corresponding value in the RStudio console. In our example
applying nchar() in str0 returns a value of 27. Each character is counted including the space
that separates two words within a given single string.

Program using nchar() function

# use nchar() function


#Returns the count of number of characters including space present in it
str0 = "welcome to Learn eTutorials" #create a character object /string str0
print(nchar(str0)) #Apply nchar() in R

The output returns the character count value.

Output:

[1] 27

Now let us consider an example using vector datatype. A vector str1 is created using c()
function with 4 elements welcome", "to", "Learn", "eTutorials" respectively. Applying
nchar() over str1 returns the count of characters in each different word or string inside the
given vector [Link] example the string “welcome” a word/element inside vector return a
value 7 after application of nchar function and so on for remaining elements too.

Program using a vector data structure to use nchar()


str1=c("welcome","to","Learn","eTutorials")
print(str1)
nchar(str1)

Output:

> print(str1)

[1] "welcome" "to" "Learn" "eTutorials"

> nchar(str1)

[1] 7 2 5 10

From the output, you can see how many characters correspond to each character string
contained in the given vector.

How to perform nchar() with NA values?


In order to deal with NA values present within a given input, an optional argument keepNA
provided in nchar() function.

Consider the code

vector0<- c(NA,"R", 'TUTORIAL', NULL)


nchar(vector0, keepNA = FALSE)

A vector named vector0 is created with a few elements inside it. Let us see first how the
vector output gets displayed.

print(vector0)
[1] NA "R" "TUTORIAL"

The above strings are displayed in the R console after executing the shortcode. Now let us
find what change does happens to the same code after applying nchar () and together with the
addition of another optional argument 'keepNA'.

> nchar(vector0)
[1] NA 1 8

The NA value is excluded from counting the characters of each string of the given vector, i.e.
vector0. The function counts the rest of the string's characters and returns the value as shown
like “R” with 1 character, “TUTORIAL” with 8 characters, and so on.

When keepNA is set to TRUE, keepNA=TRUE, produces the same result as above.

nchar(vector0, keepNA = TRUE)


[1] NA 1 8

The NA is not counted, by changing the value from TRUE to FALSE, allows the nchar()
function to count the NA if exists in the given input and returns its corresponding value.

> nchar(vector0, keepNA = FALSE)


[1] 2 1 8

The only difference between nchar() and nzchar() function is that nchar returns numeric
values whereas nzchar() returns a logical value. Consider the nzchar() applied to the same
vector0 created used in our examples, with an optional argument keepNA set to FALSE.

nzchar(vector0, keepNA = FALSE)

The output produced is

[1] TRUE TRUE TRUE

In case vectors contain any empty string represented as “ ” with a non-empty string, they will
return a FALSE value.

nchar() nzchar() Difference

vector0<- c(NA,"R", vector0<- c(NA,"R", nchar() returns a numeric


'TUTORIAL', NULL) 'TUTORIAL', NULL) vector with same length as
nchar(vector0, keepNA = nzchar(vector0, keepNA = vector(vector0) as output.
FALSE) FALSE)
[1] 2 1 8 [1] TRUE TRUE TRUE

nchar(vector0, keepNA = nzchar(vector0, keepNA = nzchar() returns a logical


TRUE) TRUE) vector with same length as
vector(vector0) as output.

[1] NA 1 8 [1] NA TRUE TRUE

[Link]() function in R

The format() function in the R programming language deals with treating all vector elements
as character strings by encoding the vector objects into a common format.

Syntax of format() function in R

format(x, trim = FALSE, digits = NULL, nsmall = 0L,


justify = c("left", "right", "centre", "none"),
width = NULL, [Link] = TRUE, scientific = NA,
[Link] = "", [Link] = 3L,
[Link] = "", [Link] = 5L,
[Link] = getOption("OutDec"),
[Link] = NULL, drop0trailing = FALSE, ...)

Where Arguments

 x any R object (conceptually); typically numeric.

 trim logical; if FALSE, logical, numeric, and complex values are right-justified to a
common width: if TRUE the leading blanks for justification are suppressed.

 digits how many significant digits are to be used for numeric and complex x. The
default, NULL, uses getOption("digits"). This is a suggestion: enough decimal places
will be used so that the smallest (in magnitude) number has this many significant
digits, and also to satisfy nsmall. (For the interpretation of complex numbers see
signif.)

 nsmall the minimum number of digits to the right of the decimal point in formatting
real/complex numbers in non-scientific formats. Allowed values are 0 <= nsmall <=
20.

 justify should a character vector be left-justified (the default), right-justified,


centered, or left alone. Can be abbreviated.
 width default method: the minimum field width or NULL or 0 for no restriction.

 [Link] is logical: should NA strings be encoded? Note this only applies to


elements of character vectors, not to numerical, complex nor logical NAs, which are
always encoded as "NA".

 scientific Either a logical specifying whether elements of a real or complex vector


should be encoded in scientific format or an integer penalty (see options("scipen")).
Missing values correspond to the current default penalty.

 ... further arguments passed to or from other methods.

[Link], [Link], [Link], [Link], [Link], [Link], drop0trailing


used for prettying (longish) numerical and complex sequences. Passed to prettyNum:

Example1: format() with arguments x,width,justify to format a string.

#use format() in R
# Place string to the left side
StrFormat1 <- format("Learn eTutorials", width = 25, justify = "l")
# Place string to the center
StrFormat2 <- format("Learn eTutorials", width = 25, justify = "c")
# Place string to the right
StrFormat3 <- format("Learn eTutorials", width = 25, justify = "r")
# Display the different string placement
print(StrFormat1)
print(StrFormat2)
print(StrFormat3)

Output

[1] "Learn eTutorials "

[1] " Learn eTutorials "

[1] " Learn eTutorials"

Example2: format() with arguments x(number),digits,nsmall,width,justify to format


numbers.

# R program to illustrate format function

# Calling the format() function over different arguments

# Rounding off the specified digits


numformat1 = format(1.45677, width = 10,digits=2)
numformat2 = format(1.45677,width = 10, digits=4)
numformat3 = format(1.45677,width = 10, justify = "r" ,digits=4)

print(numformat1)
print(numformat2)
print(numformat3)

# Getting the specified minimum number of digits


# to the right of the decimal point.
numformat3 = format(1.45677, nsmall=3)
numformat4 = format(1.45677, nsmall=7)
print(numformat3)
print(numformat4)

Output:

[1] " 1.5"

[1] " 1.457"

[1] " 1.457"

[1] "1.45677"

[1] "1.4567700"

The most useful arguments in format () to format a string is

 width To produce minimum width.

 trim No padding with spaces when set to TRUE

 justify Takes the values "left", "right", "centre", and "none" to control the padding in
strings.

The below arguments are useful for controlling the printing of numbers,

 digits The number of digits to the right of the decimal place.

 scientific use TRUE for scientific notation, FALSE for standard notation

[Link]() function in R

In R, the function substr() extracts and returns part of a string from the whole given input
string. For the process of extracting a part from a string, a start and stop integer needs to be
considered. When the substr() is applied to a string, the extraction begins with the starting
integer till it reaches the stop or ending integer. Once it reaches the referred stop integer the
function returns the extracted substring.

Syntax

substr(x, start, stop)

Where Arguments

 X is a character vector or input string.

 Start is an integer, represents the first element from where extraction begins

 Stop is an integer, that represents the last element where extraction terminates.

Consider the example code

str = "hello Learn eTutorials learners"


substr(str,6,21)

The part of the string after extraction of string str from starting integer 6 and terminating
integer 21 is

[1] " Learn eTutorial"

Similarly, a vector does perform in the same manner. Consider a vector str0 with a list of
elements or strings like "hello"," Learn"," eTutorials"," learners".

str0=c("hello"," Learn"," eTutorials"," learners")


substr(str,6,21)

[Link] grep() function in R

The grep function or grep() in R facilitates the task of identifying or searching a specific
pattern within a string. The grep function returns the number of instances of a searching
pattern if they do find a match similar to the pattern in the string.

The grep() is a pattern matching and replacement function used in R. The grep, grepl,
regexpr, gregexpr, regexec and gregexec search for matches to argument pattern within each
element of a character vector:

sub and gsub perform replacement of the first and all matches respectively.

Syntax of grep() function in R

grep(pattern, x, [Link] = FALSE, perl = FALSE, value = FALSE,


fixed = FALSE, useBytes = FALSE, invert = FALSE)
grepl(pattern, x, [Link] = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)

Where Arguments

 pattern is a character string that acts as a keyword to find a corresponding match


among strings or given character vector.

 x is the input character vector from which the pattern needs to be sought or found.

 [Link] is a boolean value set for the case sensitivity option while searching a
pattern.

[Link]=TRUE Ignores case-sensitivity while pattern matching. Eg pattern =”


Learn” Match with all other possible patterns irrespective of its
case representation “learn”, “LEARN” etc.

[Link]=FALSE Includes the case sensitivity while finding a match. Eg pattern =”


Learn” Match only with “Learn” not with “learn”, “LEARN” etc.
By default use FALSE against case sensitive option.

 perl is a logical value either TRUE OR FALSE to identify whether Perl-compatible


regexps be used or not.

 value is logical to determine whether the output should return the position of the
matching pattern.

 fixed is logical.

fixed=TRUE the pattern is a string to be matched as it is., which indicates pattern


matching needs to be exact

fixed=FALSE No restriction upon exact pattern match (default)

 useBytes is logical to show that in the case of TRUE the pattern is matched byte-by-
byte else character-by-character.

useBytes=TRUE byte-by-byte pattern matching

useBytes=FALSE character-by-character matching


 invert is logical to show that should output displays elements that do not match with
the pattern or those match with the pattern.

invert = TRUE If TRUE returns indices or values for elements that do not match.

Invert = FALSE If FALSE returns indices or values for elements that do match.

Example of grep() function


Consider the code below a vector named str0 with the following elements "hello"," Learn","
eTutorials"," Learners"

str0=c("hello"," Learn"," eTutorials"," Learners")

Suppose you need to check a pattern “Learn” in the vector str0. You can use the grep() here.

grep("Learn",str0)

The output after applying to grep() in str0 is

[1] 2 4

The grep() searches pattern & returns a number of instances. For example, the search pattern
“Learn” return the number of instances it occurs at 2, 4.

Example of grep() using argument [Link]


Consider the input character vector str0 with a list of character elements or strings.
str0=c("hello"," Learn"," eTutorials"," learners","LEARN","learN")

The table below shows use cases of [Link] with grep()

grep("Learn",str0) [1] 2 By default [Link] is FALSE ignores case


sensitive strings while pattern matching.

grep("Learn",str0,[Link] = [1] 2 if FALSE, the pattern matching is case sensitive


FALSE)

grep("Learn",str0,[Link] = [1] 2 4 if TRUE, case is ignored during matching.


TRUE) 56

Example of grep() using arguments value,fixed,usebytes,invert

str0=c("hello"," Learn"," eTutorials"," learners","LEARN","learN")


> grep("Learn",str0,[Link] = TRUE,value = TRUE,fixed = TRUE,useBytes =
TRUE,invert =TRUE)
[1] "hello" " eTutorials" " learners" "LEARN" "learN"

Description against each argument in grep() for the above example

pattern "Learn"

x str0

[Link] = TRUE Ignores case sensitivity and returns all matching patterns
value = TRUE Returns matching elements itself not indices of matched elements,

fixed = TRUE Exact match is returned

useBytes = TRUE Byte-by-byte matching

invert =TRUE Returns values that do not match in output.

The output after applying to grep() in the str0 vector is

[1] "hello" " eTutorials" " learners" "LEARN" "learN"

where these are patterns that do not match with the given pattern.

Consider what happens when all arguments in the above code are set to FALSE.

grep("Learn",str0,[Link] = FALSE,value = FALSE,fixed = FALSE,useBytes =


FALSE,invert = FALSE)
[1] 2

The value if FALSE, a vector containing the (integer) indices of the matches determined by
grep() is returned. The pattern “Learn” matches exactly with indices 2 of the str0 vector.

Another simple example is to better understand the argument's value and invert. Returns non-
matching elements as themselves ie as character vectors. The indices are not returned in these
cases.
Try to understand each argument and observe the changes from below given code.

> str0=c("hello"," Learn"," eTutorials"," learners","LEARN","learN") # vector str0


> str0
[1] "hello" " Learn" " eTutorials" " learners" "LEARN"
[6] "learN"
> grep("Learn",str0) # grep() to extract pattern
[1] 2
> grep("Learn",str0,invert = TRUE) #Non matching elements are extracted using invert
[1] 1 3 4 5 6
> grep("Learn",str0,value = TRUE,invert = TRUE) #value return character vector itself
not indices.
[1] "hello" " eTutorials" " learners" "LEARN" "learN"
>

[Link] strsplit() function in R

The strsplit() in R is a function to split the elements of a character vector. The strsplit() splits
the given character vector(string) x into substrings based on the split argument provided
within the syntax. The split argument indicates the character vector upon which the string is
split into substrings.

Syntax of strsplit() function in R

strsplit(x, split, fixed = FALSE, perl = FALSE, useBytes = FALSE)

Where the argument

 x is the input character string.

 split is the character vector used for splitting the string.

 fixed is logical. If TRUE match split exactly, otherwise use regular expressions

 perl is logical to show whether Perl-compatible regexps be used?

 useBytes is logical to indicate whether the pattern matching needs to be done byte-by-
byte or character-by-character.

Consider the character variable or string str1 "hello Learn eTutorials learners" applying
strsplit() with the split argument as “ ” returns a list of characters or elements of the string by
splitting the string str1 at the blank spaces as "hello" "Learn" "eTutorials" "learners".

str1 = "hello Learn eTutorials learners"


> print(str1)
[1] "hello Learn eTutorials learners"
> strsplit(str1, " " )
[[1]]
[1] "hello" "Learn" "eTutorials" "learners"
Consider another example to find the purpose of strsplit()

str1 = "hello Learn eTutorials learners"


print(str1)
strsplit(str1, "Learn eTutorials" )

The string is split at the position mentioned by argument split, here split is “Learn
eTutorials”.Let us find its output

[1] "hello" "learners"

[Link] tolower() and toupper() in R


The tolower() in R turns the character string into lowercase. The just opposite of tolower() is
done by function toupper. The toupper() turns the character string to uppercase.

Description tolower() & toupper() translates characters in character vectors


from upper to lower case and vice versa.

SYNTAX tolower(x) toupper(x) where x is input character string.

Example tolower(x) x= "HELLO" > print(x) [1] "HELLO" > tolower(x) [1] "hello"

toupper(x) > x= "hello" > print(x) [1] "hello" > toupper(x) [1] "HELLO"

Extracting substring

substring() function in R Programming Language is used to extract substrings in a character


vector. You can easily extract the required substring or character from the given string.
Syntax: substring(text, first, last)
Parameters:
 text: character vector
 first: integer, the first element to be replaced
 last: integer, the last element to be replaced
R – substring() Function Example

Example 1: Extracting values with substring function in R Programming language

# R program to illustrate
# substring function
# Calling substring() function
substring("Geeks", 2, 3)
substring("Geeks", 1, 4)
substring("GFG", 1, 1)
substring("gfg", 3, 3)

Output :
[1] "ee"
[1] "Geek"
[1] "G"
[1] "g"

What is String Manipulation ?

String manipulation comprises a series of functions used to extract information from text
variables. In machine learning, these functions are being widely used for doing feature
engineering, i.e., to create new features out of existing string features. In R, we have
packages such as stringr and stringi which are loaded with all string manipulation functions.

R also comprises several base functions for string manipulations. These functions are
designed to complement regular expressions. The practical differences between string
manipulation functions and regular expressions are

1. We use string manipulation functions to do simple tasks such as splitting a string,


extracting the first three letters, etc.. We use regular expressions to do more
complicated tasks such as extract email IDs or date from a set of text.
2. String manipulation functions are designed to respond in a certain way. They don't
deviate from their natural behavior. Whereas, we can customize regular expressions in
any way we want.

For example, suppose you are given a data set comprising the name of the customer as a
variable. In this case, we can use string manipulation functions to extract and create new
features as first name and last name. From the next section onward, we'll learn string
manipulation functions and commands practically. So, make sure you've R installed in your
machine. Also, you should install stringr R package.

List of String Manipulation Functions

In R, a string is any value enclosed in quotes (" "). Yes, you can even have number as strings.
R notifies strings under the class character. Let's see!

text <- "san francisco"


typeof(text)
[1] "character"

num <- c("24","34","36")


typeof(num)
[1] "character"

R's base paste function is used to combine (or paste) set of strings. In machine learning, it is
quite frequently used in creating / re-structuring variable names. For example, let's say, you
want to use two strings (Var1 and Var2) to create a new string Var3. For neatness, we'll
separate the resultant values using a - (hyphen).

var3 <- paste("Var1","Var2",sep = "-")


var3
[1] "Var1-Var2"

commonly used base R functions (also available in stringr) to modify strings:

Functions Description

It counts the number of characters in a string or vector. In the stringr package,


nchar()
it's substitute function is str_length()

It converts a string to the lower case. Alternatively, you can also use the
tolower()
str_to_lower() function

It converts a string to the upper case. Alternatively, you can also use the
toupper()
str_to_upper() function

It is used to replace each character in a string. Alternatively, you can use


chartr()
str_replace() function to replace a complete string

It is used to extract parts of a string. Start and end positions need to be


substr()
specified. Alternatively, you can use the str_sub() function

setdiff() It is used to determine the difference between two vectors

setequal() It is used to check if the two vectors have the same string values

It is used to abbreviate strings. The length of abbreviated string needs to be


abbreviate()
specified

It is used to split a string based on a criterion. It returns a list. Alternatively,


strsplit() you can use the str_split() function. This function lets you convert your list
output to a character matrix

sub() It is used to find and replace the first match in a string


It is used to find and replace all the matches in a string / vector. Alternatively,
gsub()
you can use the str_replace() function

To look at the list of all functions contained in the stringr package, go here.

library(stringr)
string <- "Los Angeles, officially the City of Los Angeles and often known by its initials
L.A., is the second-most populous city in the United States (after New York City), the most
populous city in California and the county seat of Los Angeles County. Situated in Southern
California, Los Angeles is known for its Mediterranean climate, ethnic diversity, sprawling
metropolis, and as a major center of the American entertainment industry."

strwrap(string)

#count number of characters


nchar(string)
str_length(string)

#convert to lower
tolower(string)
str_to_lower(string)

#convert to upper
toupper(string)
str_to_upper(string)

#replace strings
chartr("and","for",x = string) #letters a,n,d get replaced by f,o,r
str_replace_all(string = string, pattern = c("City"),replacement = "state") #this is case
sentitive

#extract parts of string


`substr(x = string,start = 5,stop = 11)

#extract angeles str_sub(string = string, start = 5, end = 11)

#get difference between two vectors


setdiff(c("monday","tuesday","wednesday"),c("monday","thursday","friday"))

#check if strings are equal


setequal(c("monday","tuesday","wednesday"),c("monday","tuesday","wednesday"))
setequal(c("monday","tuesday","thursday"),c("monday","tuesday","wednesday"))

#abbreviate strings
abbreviate(c("monday","tuesday","wednesday"),minlength = 3)

#split strings
strsplit(x = c("ID-101","ID-102","ID-103","ID-104"),split = "-")
str_split(string = c("ID-101","ID-102","ID-103","ID-104"),pattern = "-",simplify = T)
#find and replace first match
sub(pattern = "L",replacement = "B",x = string,[Link] = T)

#find and replace all matches


gsub(pattern = "Los",replacement = "Bos",x = string,[Link] = T)

The pattern parameter in the functions above also accept regular expressions. These functions
when combined with regular expressions can do highly complex search operations. Now, let's
learn about regular expressions.

List of Regular Expression Commands

Apart from the function listed above, there are several other functions specially designed to
deal with regular expressions (a.k.a regex). Yes, R is equally powerful when it comes to
parsing text data. In regex, there are multiple ways of doing a certain task. Therefore, while
learning, it's essential for you to stick to a particular method to avoid confusion.

For using regular expressions, the available base regex functions are grep(), grepl(),
regexpr(), gregexpr(), regexec(), and regmatches(). Here's a quick preview of these
functions:

Function Description

Grep returns the index or value of the matched string

Grepl returns the Boolean value (True or False) of the matched string

Regexpr return the index of the first match

Gregexpr returns the index of all matches

Regexec is a hybrid of regexpr and gregexpr

returns the matched string at a specified index. It is used in conjunction


regmatches
with regexpr and gregexpr.

Regular expressions in R can be divided into 5 categories:


1. Metacharacters
2. Sequences
3. Quantifiers
4. Character Classes
5. POSIX character classes

1. Metacharacters

Metacharacters comprises a set of special operators which regex doesn't capture. Yes, regex
work by its own rules. These operators are most common in every line of text you would
come across. These characters include: . \ | ( ) [ ] { } $ * + ?

If any of these characters are available in a string, regex won't detect them unless they are
prefixed with double backslash (\) in R. Let's see how to escape these characters in R.

From a given vector, we want to detect the string "percent%." We'll use the
base grep() function used to detect strings given a pattern. Also. we'll use the gsub() function
to make the replacements. We can do it like this:

dt <- c("percent%","percent")
grep(pattern = "percent\\%",x = dt,value = T)
[1] "percent%"

#detect all strings


dt <- c("may?","money$","and&")
grep(pattern = "[a-z][\\?-\\$-\\&]",x = dt,value = T)
[1] "may?" "money$" "and&"

gsub(pattern = "[\\?-\\$-\\&]",replacement = "",x = dt)


[1] "may" "money" "and"

In fact, if you find a double backslash in a string, you'll need to prefix it with another double
backslash to get detected. Following is an example:

gsub(pattern = "\\\\",replacement = "-",x = "Barcelona\\Spain")


[1] "Barcelona-Spain"

2. Quantifiers

Quantifiers are the shortest to type, but these tiny atoms are immensely powerful. One
position here and there can change the entire output value. Quantifiers are mainly used to
determine the length of the resulting match. Always remember, that quantifiers exercise their
power on items to the immediate left of it. Following is the list of quantifiers commonly used
in detecting patterns in text: It matches everything except a newline.

Quantifier Description
. It matches everything except a newline.

? The item to its left is optional and is matched at most once.

* The item to its left will be matched zero or more times.

+ The item to its left is matched one or more times.

The item to its left is matched exactly n times. The item must have a
{n}
consecutive repetition at place. e.g. Anna

{n, } The item to its left is matched n or more times.

{n,m} The item to its left is matched at least n times but not more than m times.

These quantifiers can be used with metacharacters, sequences, and character classes to
return complex patterns. Combinations of these quantifiers help us match a pattern. The
nature of these quantifiers is better known in two ways:

 Greedy Quantifiers : The symbol .* is known as a greedy quantifier. It says that for a
particular pattern to be matched, it will try to match the pattern as many times as its
repetition are available.
 Non-Greedy Quantifiers : The symbol .? is known as a non-greedy quantifier. Being
non-greedy, for a particular pattern to be matched, it will stop at the first match.

The desired result is 101.

number <- "101000000000100"

#greedy
regmatches(number, gregexpr(pattern = "1.*1",text = number))
[1] "1010000000001"

#non greedy
regmatches(number, gregexpr(pattern = "1.?1",text = number))
[1] "101"

3. Sequences
As the name suggests, sequences contain special characters used to describe a pattern in a
given string. Following are the commonly used sequences in R:

Sequences Description

\d matches a digit character

\D matches a non-digit character

\s matches a space character

\S matches a non-space character

\w matches a word character

\W matches a non-word character

\b matches a word boundary

\B matches a non-word boundary

string <- "I have been to Paris 20 times"

#match a digit
gsub(pattern = "\\d+",replacement = "_",x = string)
regmatches(string,regexpr(pattern = "\\d+",text = string))

#match a non-digit
gsub(pattern = "\\D+",replacement = "_",x = string)
regmatches(string,regexpr(pattern = "\\D+",text = string))

#match a space - returns positions


gregexpr(pattern = "\\s+",text = string)

#match a non space


gsub(pattern = "\\S+",replacement = "app",x = string)
#match a word character
gsub(pattern = "\\w",replacement = "k",x = string)

#match a non-word character


gsub(pattern = "\\W",replacement = "k",x = string)

4. Character Classes

Character classes refer to a set of characters enclosed in a square bracket [ ]. These classes
match only the characters enclosed in the bracket. These classes can also be used in
conjunction with quantifiers. The use of the caret (^) symbol in character classes is
interesting. It negates the expression and searches for everything except the specified pattern.
Following are the types of character classes used in regex:

Characters Description

[aeiou] matches lower case vowels

[AEIOU] matches upper case vowels

[0123456789] matches any digit

[0-9] same as the previous class

[a-z] match any lower case letter

[A-Z] match any upper case letter

[a-zA-Z0-9] match any of the above classes

string <- "20 people got killed in the mob attack. 14 got severely injured"

#extract numbers
regmatches(x = string,gregexpr("[0-9]+",text = string))

#extract without digits


regmatches(x = string,gregexpr("[^0-9]+",text = string))
5. POSIX Character Classes

In R, these classes can be identified as enclosed within a double square bracket ([[ ]]). They
work like character classes. A caret ahead of an expression negates the expression value. I
find these classes more intuitive than the rest, and hence easier to learn. Following are the
posix character classes available in R:

POSIX
Description
Characters

[[:lower:]] matches lower case letter

[[:upper:]] matches upper case letter

[[:alpha:]] matches letters

[[:digit:]] matches digits

[[:space:]] matches space characters eg. tab, newline, vertical tab, space, etc

[[:blank:]] matches blank characters (same as previous) such as space, tab

[[:alnum:]] matches alphanumeric characters, e.g. AB12, ID101, etc

matches control characters. Control characters are non-printable


[[:cntrl:]] characters such as \t (tab), \n (new line), \e (escape), \f (form feed),
etc

[[:punct:]] matches punctuation characters

Let's look at some of the examples of this regex class:

string <- c("I sleep 16 hours\n, a day","I sleep 8 hours\n a day.","You sleep how many\t hours
?")
#get digits
unlist(regmatches(string,gregexpr("[[:digit:]]+",text = string)))

#remove punctuations
gsub(pattern = "[[:punct:]]+",replacement = "",x = string)

#remove spaces
gsub(pattern = "[[:blank:]]",replacement = "-",x = string)

#remove control characters


gsub(pattern = "[[:cntrl:]]+",replacement = " ",x = string)

#remove non graphical characters


gsub(pattern = "[^[:graph:]]+",replacement = "",x = string)

You might also like