Removing specific repetitions of values within a group

I have this dataset and I want to group them by Name delete the second repetition of the verb "eat" , the third repetition of the verb "sleep", and the first repetition of the verb "hit".

u <- data.frame(Name = c("Jon", "Jon", "Jon","Jon", "Bill", "Bill","Bill", "Bill", "Maria","Maria", "Maria", "Maria","Maria", "James", "James"),
                Age = c(23, 41, 32, 58, 26, 45,86,13,86,09,23,41,27,06, 42), 
                verb = c(rep('eat',4),rep('sleep',4), c('go'),c('dislike'), c('pull'),rep('hit',4)))

The output I am looking for is something like:

id Name Age    verb
1    Jon  23     eat
3    Jon  32     eat
4    Jon  58     eat
5   Bill  26   sleep
6   Bill  45   sleep
8   Bill  13   sleep
9  Maria  86      go
10 Maria   9 dislike
11 Maria  23    pull
13 Maria  27     hit
14 James   6     hit
15 James  42     hit

Thank you in advance!

Hi @Dallak

Here's a way to do this:

library(dplyr)
u %>% 
  group_by(Name, verb) %>% 
  mutate(
    occurence = row_number(),
    delete = ifelse(verb == "eat" & occurence ==2 | verb == "sleep" & occurence ==3 | verb == "hit" & occurence ==1, TRUE, FALSE)
    ) %>% 
  filter(delete == FALSE) %>% 
  select(-occurence, -delete) %>% 
  ungroup()

Only difference with your desired output is the first occurence of verb "hit" for James that is removed.

1 Like

Thank you, @xvalda. this is fantastic!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.