Questions tagged [grepl]

grepl is an R function that searches for matches to argument pattern within each element of a character vector.

grepl returns TRUE if a string contains the pattern, otherwise FALSE; if the parameter is a string vector, returns a logical vector (match or not for each element of the vector).

Description

grepl(pattern, x, ignore.case = FALSE, perl = FALSE,
      fixed = FALSE, useBytes = FALSE)
  • pattern: regular expression, or string for fixed=TRUE

  • x: string, the character vector

  • ignore.case: case sensitive or not

  • perl: logical. Should perl-compatible regexps be used? Has priority over extended

  • fixed: logical. If TRUE, pattern is a string to be matched as is. Overrides all conflicting arguments

  • useBytes: logical. If TRUE the matching is done byte-by-byte rather than character-by-character

Example

> x <- "line 4322: He is now 25 years old, and weights 130lbs"
> y <- grepl("\\d+",x)
> y

Reference

  • R Documentation
769 questions
45
votes
1 answer

Filtering observations in dplyr in combination with grepl

I am trying to work out how to filter some observations from a large dataset using dplyr and grepl . I am not wedded to grepl, if other solutions would be more optimal. Take this sample df: df1 <- data.frame(fruit=c("apple", "orange", "xapple",…
jalapic
  • 13,792
  • 8
  • 57
  • 87
40
votes
1 answer

Search within a string that does not contain a pattern

It appears that while grep has an invert argument, grepl does not. I would like to subset for using 2 filters data$ID[grepl("xyx", data$ID) & data$age>60] How can I subset for age>60 and ID not containing "xyx"? What I did…
ECII
  • 10,297
  • 18
  • 80
  • 121
27
votes
3 answers

grepl in R to find matches to any of a list of character strings

Is it possible to use a grepl argument when referring to a list of values, maybe using the %in% operator? I want to take the data below and if the animal name has "dog" or "cat" in it, I want to return a certain value, say, "keep"; if it doesn't…
Marc Tulla
  • 1,751
  • 2
  • 20
  • 34
24
votes
3 answers

Complete word matching using grepl in R

Consider the following example: > testLines <- c("I don't want to match this","This is what I want to match") > grepl('is',testLines) > [1] TRUE TRUE What I want, though, is to only match 'is' when it stands alone as a single word. From reading a…
aaron
  • 6,339
  • 12
  • 54
  • 80
21
votes
3 answers

R's grepl() to find multiple strings exists

grepl("instance|percentage", labelTest$Text) will return true if any one of instance or percentage is present. How will I get true only when both the terms are present?
toofrellik
  • 1,277
  • 4
  • 15
  • 39
19
votes
2 answers

R how to use grep in if statement

In R I want to do a like in an if statement like the example below where I'm searching for any colors in the mix$color column that contain the word red and setting a new variable in the mix dataframe to the color red. mix$newcolor <-…
Jazzmine
  • 1,837
  • 8
  • 36
  • 54
19
votes
2 answers

grepl for a period "." in R?

Lets say I have a string "Hello." I want to see if this string contains a period: text <- "Hello." results <- grepl(".", text) This returns results as TRUE, but it would return that as well if text is "Hello" without the period. I'm confused, I…
marc
  • 2,037
  • 9
  • 24
  • 32
18
votes
7 answers

Regex to detect if all alphabetic characters are upper case

How would I go about detecting if all alphabetic characters in a string (of >= 2 characters) are upper case? Ultimately, I'm trying to filter out chapter title names, that are rows in my data-set. So if a chapter title is "ARYA", I want that…
Evan O.
  • 1,553
  • 2
  • 11
  • 20
17
votes
2 answers

Is it possible to use an AND operator in grepl()?

I want to search for anything that begins with 55 and anything that has the word Roof (case-sensitive, for those who are curious) in it. So far I have been unsuccessful, as I can only seem to use the OR operator: grepl("*^55|*Roof",…
Mus
  • 7,290
  • 24
  • 86
  • 130
15
votes
2 answers

Use grepl to search either of multiple substrings in a text

I am using grepl() in R to search if either of the following genres exist in my text. I am doing it like this right now: grepl("Action", my_text) | grepl("Adventure", my_text) | grepl("Animation", my_text) | grepl("Biography", my_text)…
user3422637
  • 3,967
  • 17
  • 49
  • 72
13
votes
2 answers

Fast way to group variables based on direct and indirect similarities in multiple columns

I have a relatively large data set (1,750,000 lines, 5 columns) which contains records with unique ID values (first column), described by four criteria (4 other columns). A small example would be: # example library(data.table) dt <-…
R. Lima
  • 412
  • 4
  • 15
13
votes
3 answers

Remove entries from string vector containing specific characters in R

I've got two character vectors: x = {"a", "b", "c", "kt"} y = {"abs", "kot", "ccf", "okt", "kk", "y"} I need to use x to remove entries from y so that only the strings that do not contain any of the x's entries remain, like this: y = {"kot", "kk",…
Lecromine
  • 178
  • 1
  • 2
  • 10
12
votes
3 answers

Exact match with grepl R

I'm trying to extract certain records from a dataframe with grepl. This is based on the comparison between two columns Result and Names. This variable is build like this "WordNumber" but for the same word I have multiple numbers (more than 30), so…
Barbara
  • 1,118
  • 2
  • 11
  • 34
12
votes
1 answer

Subset based on list of strings using grepl()?

I'm looking to do something seemingly very simple. I would like to subset a data frame in R using the grepl() command -- or something like it -- on several different phrases without constructing a loop. For example, I'd like to pull out all the…
baha-kev
  • 3,029
  • 9
  • 33
  • 31
11
votes
1 answer

grepl for dplyr sql table?

is there a workaround to use something like filter(df, grepl("A|B|C",location)) for a dplyr SQL table? In SQL it is probalby a LIKE. Of cource I could convert the SQL table to a R data table, but it is very large.…
ckluss
  • 1,477
  • 4
  • 21
  • 33
1
2 3
51 52