<- data.frame(
data id = 1:5,
name = c("apple", "banana", "cherry", "date", "elderberry"),
stringsAsFactors = FALSE
)print(data)
id name
1 1 apple
2 2 banana
3 3 cherry
4 4 date
5 5 elderberry
Steven P. Sanderson II, MPH
May 23, 2024
Good morning, everyone!
Today, we’re going to talk about how to handle rows in your dataset that contain a specific string. This is a common task in data cleaning and can be easily accomplished using both base R and the dplyr
package. We’ll go through examples for each method and break down the code so you can understand and apply it to your own data.
First, let’s see how to select and drop rows containing a specific string using base R. We’ll use the grep()
function for this.
Let’s create a simple data frame to work with:
Suppose we want to select rows where the name contains the letter “a”. We can use grep()
:
id name
1 1 apple
2 2 banana
4 4 date
Explanation:
grep("a", data$name)
searches for the letter “a” in the name
column and returns the indices of the rows that match.data[grep("a", data$name), ]
uses these indices to subset the original data frame.To drop rows that contain the letter “a”, we can use the -grep()
notation:
Explanation:
-grep("a", data$name)
returns the indices of the rows that do not match the search term.data[-grep("a", data$name), ]
subsets the original data frame by excluding these rows.The dplyr
package makes these tasks even more straightforward with its intuitive functions.
We’ll use the same data frame as before. First, make sure you have dplyr
installed and loaded:
Using dplyr
, we can select rows containing “a” with the filter()
function combined with str_detect()
from the stringr
package:
library(stringr)
selected_rows_dplyr <- data %>%
filter(str_detect(name, "a"))
print(selected_rows_dplyr)
id name
1 1 apple
2 2 banana
3 4 date
Explanation:
%>%
is the pipe operator, allowing us to chain functions together.filter(str_detect(name, "a"))
filters rows where the name
column contains the letter “a”.To drop rows containing “a” using dplyr
, we use filter()
with the negation operator !
:
id name
1 3 cherry
2 5 elderberry
Explanation:
!str_detect(name, "a")
negates the condition, filtering out rows where the name
column contains the letter “a”.Both base R and dplyr
provide powerful ways to select and drop rows based on specific strings. The grep()
function in base R and the combination of filter()
and str_detect()
in dplyr
are versatile tools for your data manipulation needs.
Give these examples a try with your own datasets! Experimenting with different strings and data structures will help reinforce these concepts and improve your data manipulation skills.
Happy coding!