How to filter data by a text pattern in R

Asked

Viewed 114 times

1

I have a database with data of Brazilian cities and states, I would like to filter only the states, all follow a pattern of having a "br_states_" before the name of the state. I tried to use the package string and the function filter package dplyr, but to no avail.

Code I am using:

library(readr)
library(dplyr)
library(stringr)

dados <- read_csv('http://tiny.cc/idb-traffic-daily') %>%
  filter(country_name == "Brazil") %>%
  str_detect(pattern = "br_states_")
  • 2

    What was missing from your code were two things.1) the str_detect must be inside the filter. 2) You need to inform for the str_detect where he should seek the pattern, i.e., provide the first argument. Something like this: filter(..., str_detect(region_slug, "br_states_"))

1 answer

4


Use the function grep. It allows you to search string snippets. By combining it with the function filter package dplyr, it is possible to keep only rows of the column region_slug who possess br_states:

library(readr)
library(dplyr)

dados <- read_csv('http://tiny.cc/idb-traffic-daily') %>%
  filter(grepl("br_states", region_slug))

Browser other questions tagged

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