Below is the image that describes my data frame, I wish to conditionally delete all city names which have "Range" written in them as indicated in the snippet. I tried various approaches but haven't been successful so far.
Asked
Active
Viewed 2,249 times
-1

pogibas
- 27,303
- 19
- 84
- 117

Jehan Joshi
- 33
- 4
-
Some code to generate data would be much more useful than a screenshot, it would help others help you. See http://reprex.tidyverse.org for how to make a reprex. – Romain Francois Sep 23 '17 at 18:37
2 Answers
3
There are two things: detect a pattern in a character vector, you can use stringr::str_detect()
and extract a subset of rows, this is dplyr::filter()
purpose.
library(dplyr)
library(stringr)
df <- df %>%
filter( ! str_detect(City, "Range") )

Romain Francois
- 17,432
- 3
- 51
- 77
1
Use grep
with invert option to select all lines without Range
.
yourDataFrame <- yourDataFrame[grep("Range", yourDataFrame$City, invert = TRUE), ]

pogibas
- 27,303
- 19
- 84
- 117
-
You'll have troubles if `grep` returns no matches (`integer(0)`). `grepl` is safer. – Rich Scriven Sep 23 '17 at 19:10
-
Thank You so much for your solution, your solution solved my problem :) – Jehan Joshi Sep 23 '17 at 19:59