Exact match with grepl R
I think this is just:
Relationships[Relationships$Results==Names,]
If you end up doing ^Word1$
you're just doing a straight subset.
If you have multiple names, then instead use:
Relationships[Relationships$Results %in% Names,]
Using Grepl to get an exact match, whilst in a for loop
Try this:
list_df<-list()
for (i in 1:length(my_vector))
{
df<-my_df%>%
filter(grepl(paste0("\\b",my_vector[i],"\\b"),My_vehicles))
list_df[[i]]=df
}
How to use grep()/gsub() to find exact match
Use word boundary \b
which matches a between a word and non-word character,
string = c("apple", "apples", "applez")
grep("\\bapple\\b", string)
[1] 1
OR
Use anchors. ^
Asserts that we are at the start. $
Asserts that we are at the end.
grep("^apple$", string)
[1] 1
You could store the regex inside a variable and then use it like below.
pat <- "\\bapple\\b"
grep(pat, string)
[1] 1
pat <- "^apple$"
grep(pat, string)
[1] 1
Update:
paste("^",pat,"$", sep="")
[1] "^apple$"
string
[1] "apple" "apple:s" "applez"
pat
[1] "apple"
grep(paste("^",pat,"$", sep=""), string)
[1] 1
R grep and exact matches
You want to use word boundaries \b
around your word patterns. A word boundary does not consume any characters. It asserts that on one side there is a word character, and on the other side there is not. You may also want to consider using the inline (?i)
modifier for case-insensitive matching.
grep('(?i)(?=.*\\bplant\\b)(?=.*\\bcoal\\b)', df$Sources, perl=T, value=T)
Working Demo
Filter column using grepl to keep particular string match
Use ^
to match the beginning of a string, as in
df2 <- df1[,grepl("^H|^TCGA", colnames(df1))]
We can also use dplyr with starts_with()
:
library(dplyr)
df1 %>%
select(starts_with(c('H', 'TCGA'))
Pattern match with grepl() function in R
The result is correct.
grepl
is looking for the pattern of xx-xx-xx, where x is a digit, and that does appear in the first query. If you want to query starting from the beginning of the string, you can use the ^
symbol.
For example, if you were to run grepl("^[0-9]{2}-[0-9]{2}-[0-9]{2}", "2010-04-09")
, you'd get FALSE, but grepl("^[0-9]{4}-[0-9]{2}-[0-9]{2}", "2010-04-09")
would return TRUE.
PS: On the opposite end, $
indicates the end of the string.
Related Topics
Modifying Plot in Ggplot2 Using As.Yearmon from Zoo
Visualizing Distance Between Nodes According to Weights - with R
How to Create a Continuous Legend (Color Bar Style) for Scale_Alpha
Error: Attempt to Use Zero-Length Variable Name
Existing Function to Combine Standard Deviations in R
Margins Between Plots in Grid.Arrange
What's The Difference Between [1], [1,], [,1], [[1]] for a Dataframe in R
How to Format Kable Table When Knit from .Rmd to Word (With Bookdown)
How to Annotate Across or Between Plots in Multi-Plot Panels in R
How to Fix Degree Symbol Not Showing Correctly in R on Linux/Fedora 31
Can't Install Any R Packages on Linux Server
Combining Multiple Identically-Named Columns in R
Make a Column with Duplicated Values Unique in a Dataframe
Extract Only Folder Name Right Before Filename from Full Path
Get Expression That Evaluated to Dot in Function Called by 'Magrittr' Pipe
Robust Standard Errors for Mixed-Effects Models in Lme4 Package of R