Select part of Text in R

Asked

Viewed 1,848 times

1

I would like to filter specific elements of a line, similar to the Excel filter. I have the following example:

NOME    VALOR
LEITO 1 10
LEITO 2 - HPP   20
LEITO 3 - HPP   30
LEITO 4 40

I need to filter, in the column name, lines with the characters HPP. The final result of the filter should look like this:

NOME    VALOR
LEITO 2 - HPP   20
LEITO 3 - HPP   30

How do I make this filter?

1 answer

3


The packages dplyr and stringr can help you in this. First, I will create the dataset:

NOME  <- c("LEITO 1", "LEITO 2 - HPP", "LEITO 3 - HPP", "LEITO 4")
VALOR <- c(10, 20, 30, 40)
dados <- data.frame(NOME, VALOR)

Then I load the necessary packages:

library(dplyr)
library(stringr)

Finally, a combination of functions filter, which selects lines according to some criterion, and str_detect, searching for a specific snippet of characters within a larger set of characters:

dados %>% 
  filter(str_detect(NOME, "HPP"))
           NOME VALOR
1 LEITO 2 - HPP    20
2 LEITO 3 - HPP    30
  • 1

    how would the function look if I filter more than one option? For example, filter words that contain "HPP" or "HPP2". Grateful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.