# grep in R: How to Use grep() Function in R

Suppose we wanted to identify the records for all the victims of floods in China. How could we do that? Here I use grep() to match the literal into the character vector of flooded victims. Let’s see how to use grep() function in R and difference between grep() and grepl() functions.

## grep in R

The grep() is a built-in R function that searches for matches to argument patterns within each element of a character vector.

### Syntax

``````grep(pattern, x, ignore.case = FALSE, perl = FALSE, value = FALSE,
fixed = FALSE, useBytes = FALSE, invert = FALSE)``````

### Arguments

pattern: It takes a character string containing a regular expression (or character string for fixed = TRUE) to match the given character vector.

x: It is a character vector where matches are sought, or an object which can be coerced by as.character to a character vector.

ignore.case: If FALSE, the pattern matching is case sensitive, and if TRUE, the case is ignored during matching.

perl: It is a logical argument and should Perl-compatible regular expressions be used.

value: If it is a FALSE, a vector containing the (integer) indices of the matches determined by grep() is returned, and if TRUE, a vector containing the matching elements themselves is returned.

fixed: It is a logical argument, and If it is TRUE, the pattern is a string to be matched as is. Overrides all conflicting arguments.

useBytes: It is a logical argument, and if it is TRUE, the matching is done byte-by-byte rather than character-by-character.

invert: It is logical, and If it is TRUE, return indices or values for elements that do not match.

### Return Value

The grep() function returns a vector of the indices of the elements of x that yielded a match (or not, for invert = TRUE).

### Example

Let’s search character “a” in the character string.

``````data <- c("Newgen", "Happiest Minds", "Tata Elxsi", "LTTS")

print(grep("a", data))``````

#### Output

`` 2 3``

In this example, “a” is matched with Happiest Minds and Tata Elxsi. So, it returns the index of these strings.

It searches for matches of the input character “a” within the example vector data and returns the indices of vector elements that contain the character “a”.

## grep() vs. grepl() functions in R

The grepl() is a built-in function that searches for matches of a string or string vector. The grepl() function returns TRUE if a string contains the pattern, otherwise FALSE. The grep() function searches for matches of a certain character pattern.

These grep() and grepl() functions search for matches of a regular expression/pattern in a character vector. The grep() returns the indices into the character vector that contains a match or the specific strings that happen to have the match. grepl() returns a TRUE/FALSE vector indicating which elements of the character vector contain a match.

``````data <- c("Newgen", "Happiest Minds", "Tata Elxsi", "LTTS")

print(grep("a", data))

print(grepl("a", data))``````

#### Output

`````` 2 3

 FALSE TRUE TRUE FALSE``````

You can see that the grepl() function returns boolean values instead of returns the indices like the grep() function.

## Apply grep() function with Multiple Patterns

The grep() function checks for multiple character patterns in our vector of character strings.

``````data <- c("Newgen", "Happiest Minds", "Tata Elxsi", "LTTS")

print(grep("a|t", data))``````

#### Output

`` 2  3``

In this example, we are searching a or t in a string, and it found two strings that contain both character vectors.

That’s it for the grep() function in R.

Categories R
{"ft_lang_label":"__label__en","ft_lang_prob":0.7866643,"math_prob":0.95426494,"size":3430,"snap":"2021-43-2021-49","text_gpt3_token_len":802,"char_repetition_ratio":0.17688267,"word_repetition_ratio":0.06205674,"special_character_ratio":0.23440233,"punctuation_ratio":0.120300755,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96883476,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-11-30T00:33:01Z\",\"WARC-Record-ID\":\"<urn:uuid:62ebee3f-2ed4-45f0-b7df-a6c135b59ed7>\",\"Content-Length\":\"77383\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:59e128d4-dfd1-4363-8783-3f85859bbe09>\",\"WARC-Concurrent-To\":\"<urn:uuid:f87d1537-944b-4081-b32e-a5579d49845e>\",\"WARC-IP-Address\":\"143.198.65.47\",\"WARC-Target-URI\":\"https://r-lang.com/grep-in-r-how-to-use-grep-function-in-r/\",\"WARC-Payload-Digest\":\"sha1:VVDP3YTRJD2INKIBOADMZTOQLBG577JP\",\"WARC-Block-Digest\":\"sha1:RG7Z7XNY5A4WMVIINMZORYW5R5UJH7O5\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964358847.80_warc_CC-MAIN-20211129225145-20211130015145-00411.warc.gz\"}"}