Open In App

Filter rows based on text patterns using R

Last Updated : 12 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will explore various methods to filter rows based on text patterns by using the R Programming Language.

How do we filter the rows based on text patterns?

R language offers various methods to filter the rows based on text patterns in various data sets. By using these methods provided by R, it is possible to filter the rows efficiently. Some of the methods to filter the rows based on text patterns are:

By using the subset() function

This method is used to filter the rows based on the text patterns efficiently. In the below example, we created a data frame and filtered the rows based on the text patterns.

R
# Original DataFrame
data <- data.frame(
  Name = c("John", "Alice", "Bob", "Emily", "David"),
  Age = c(25, 30, 35, 28, 40),
  City = c("New York", "Los Angeles", "Chicago", "San Francisco", "Boston")
)
print("original data")
print(data)

# Filter rows where City contains "New"
filtered_data <- subset(data, grepl("New", City))

print("Filtered Data:")
print(filtered_data)

Output:

[1] "original data"
Name Age City
1 John 25 New York
2 Alice 30 Los Angeles
3 Bob 35 Chicago
4 Emily 28 San Francisco
5 David 40 Boston

[1] "Filtered Data:"
Name Age City
1 John 25 New York

In the below example, we created a data frame and filtered the rows based on the text patterns.

R
# Original DataFrame
data <- data.frame(
  Name = c("Mahesh","suresh","ramesh","kamesh","lokesh"),
   id_no= c(1125, 3034, 5635, 2768, 4075),
    college_name= c("abc", "xyz", "raghu", "aditya", "vishnu")
)
print("original data")
print(data)
# Filter rows where City contains "New"
filtered_data <- subset(data, grepl("ad", college_name))

print("Filtered Data:")
print(filtered_data)

Output:

[1] "original data"
Name id_no college_name
1 Mahesh 1125 abc
2 suresh 3034 xyz
3 ramesh 5635 raghu
4 kamesh 2768 aditya
5 lokesh 4075 vishnu

[1] "Filtered Data:"
Name id_no college_name
4 kamesh 2768 aditya

By using the grep() function

This method is used to filter the rows based on the text patterns efficiently. In the below example, we created a data frame and filtered the rows based on the text patterns.

R
# Original DataFrame
data <- data.frame(
  Name = c("John", "Alice", "Bob", "Emily", "David"),
  Age = c(25, 30, 35, 28, 40),
  City = c("New York", "Los Angeles", "Chicago", "San Francisco", "Boston")
)

print("Original data")
print(data)

# Filter rows where City contains "Los"
filtered_indices <- grep(4, data$Age)
filtered_data <- data[filtered_indices, ]

# Output
print("Filtered Data:")
print(filtered_data)

Output:

[1] "Original data"
Name Age City
1 John 25 New York
2 Alice 30 Los Angeles
3 Bob 35 Chicago
4 Emily 28 San Francisco
5 David 40 Boston

[1] "Filtered Data:"
Name Age City
5 David 40 Boston

In the below example, we created a data frame and filtered the rows based on the text patterns.

R
Name = c("jai","price","gaurav","sohit","arun")
Age = c(27,34,65,77,89)
Address = c("delhi", "kanpur", "allhabad", "raipur", "kerla")
Qualification = c("Msc", "Phd", "Mca", "B.tech", "Degree")
# Original DataFrame
data <- data.frame(Name, Age, Address, Qualification)

print("Original data")
print(data)

# Filter rows where City contains "Los"
filtered_indices <- grep("e", data$Address)
filtered_data <- data[filtered_indices, ]

print("Filtered Data:")
print(filtered_data)

Output:

[1] "Original data"
Name Age Address Qualification
1 jai 27 delhi Msc
2 price 34 kanpur Phd
3 gaurav 65 allhabad Mca
4 sohit 77 raipur B.tech
5 arun 89 kerla Degree

[1] "Filtered Data:"
Name Age Address Qualification
1 jai 27 delhi Msc
5 arun 89 kerla Degree

Conclusion

In conclusion, we learned about how to filter the rows based on text patterns in R. R language offers versatile tools while performing the filter concepts.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads