-2
id: 1 1 2 2 3 3
key: ab a*b c*d*e cde a*3 3 

I simply want id as 1 2 3 and key as ab cde 3.

So basically remove each record with * in the key.

Thanks

nniloc
  • 4,128
  • 2
  • 11
  • 22
  • for key variable: some of the records are with * sign. like a*b, c*d*e, a*3 – user13815308 Sep 25 '20 at 16:43
  • Are you looking to remove the `*` in `key`? Try `gsub('\\*', '', key)` – nniloc Sep 25 '20 at 18:17
  • I want to delete that entire now where key has *. So i have two count of 6 rows, where 3 of those rows have key with * values, so i basically want the remaining 3 rows records without *. Hope that makes sense..thank you. – user13815308 Sep 25 '20 at 19:34
  • Next time it would be useful to post your data using `dput`. Here are some tips on how to create a minimal reproducible example for your question: https://stackoverflow.com/a/5963610/12400385 – nniloc Sep 25 '20 at 19:43

1 Answers1

1

You can use grepl to find all values of key with a *. Because * is a special character, you have to use \\ to match it. ! will negate the match.

The dplyr way

library(dplyr)

df %>% filter(!grepl('\\*', key))

#----------
  id key
1  1  ab
4  2 cde
6  3   3

Or using the full tidyverse which gives your stringr::str_detect

library(tidyverse)

df %>% filter(str_detect(key, '\\*', negate = TRUE))

Equivalents in base R

subset(df, !grepl('\\*', key))

Or

df[!grepl('\\*', df$'key'),]

# Data
df <- data.frame(id= c(1, 1, 2, 2, 3, 3),
                 key= c('ab', 'a*b', 'c*d*e', 'cde', 'a*3', 3 ))
nniloc
  • 4,128
  • 2
  • 11
  • 22