0

I've seen code for cells that contain exactly a string, and code for cells that contain a string (i.e. the string is a substring of the cell data) but how can I remove rows in a dataframe that start (in that column) with a specific string ?

For instance

Name           Number
John Smith     4
John Lee       3
James Michaels 2
Johnny King    7

would return

James Michaels 2

if all rows which start with "John" in the Name column are removed.

Kara
  • 6,115
  • 16
  • 50
  • 57
user1569897
  • 437
  • 1
  • 5
  • 12
  • 3
    `^` in regex represents the start of a string, so `dat[!grepl("^John", dat$Name),]` I think. This question is a possible duplicate too - https://stackoverflow.com/questions/35684819/r-how-to-remove-rows-in-a-data-frame-based-on-the-first-character-of-a-column – thelatemail Jul 21 '17 at 01:08
  • 2
    There is a function `startsWith` that will be useful to you here. Check the help file. :) – Rich Scriven Jul 21 '17 at 01:14
  • @RichScriven - on an older version of R here at the moment - is `startsWith` case sensitive? – thelatemail Jul 21 '17 at 01:25
  • @thelatemail - [around version 3.3.0](https://stackoverflow.com/questions/31467732/does-r-have-function-startswith-or-endswith-like-python/38151842#38151842), and yes – Rich Scriven Jul 21 '17 at 01:30

0 Answers0