How to use stringr package In R Language
The stringr package in R language is used mainly for character manipulations, locale-sensitive operations, altering whitespace, and Pattern-matching. Here we will use its pattern matching functionality to filter data according to partial string match.
Syntax:
df[str_detect(df$column-name, “Pattern”), ]
Parameters:
- df: determines the dataframe that is being used.
- column-name: determines the column in which strings have to be filtered.
- Pattern: determines the string pattern that has to be matched.
Example: This example explains how to extract rows with a partial match using the stringr package.
R
# Load library stringr library ( "stringr" ) # sample dataframe data<- data.frame (names= c ( 'Hello' , 'this' , 'Hell' , 'Geeks' , 'Geek' , 'w3wiki' )) # Filter data with str_detect for strings # containing "Gee" result1<-data[ str_detect (data$name, "Gee" ), ] # print result data result1 # Filter data with str_detect for strings # containing "Hel" result2<-data[ str_detect (data$name, "Hel" ), ] # print result data result2 |
Output:
[1] “Geeks” “Geek” “w3wiki”
[1] “Hello” “Hell”
Select Rows with Partial String Match in R DataFrame
In this article, we’ll discuss how to select rows with a partial string match in the R programming language.