Q:

R - eliminating duplicate values

I have an input dataframe like this:

I want the output to be like this:

For example, I want to take up the first value(mary has life), scan it against all other rows which have duplicate COL1 entries and if a duplicate COL2 value is present I need to eliminate duplicates alone while merging non-duplicates.  In other words, I want to do pattern search. If the same pattern is present in another row, I just want to eliminate duplicate patterns and merge non-duplicate patterns.
I tried using the grepl and gsub functions but I am not able to get my desired result properly.
Inserting a simpler version of input dataset below:
COL1    COL2
10  mary has life
10  Don mary has life
10  Britto mary has life
20  push them
20  push them fur
30  yell at this
30  this is yell at this
40  Year
40  Doggy
40  Horse

A:

After your update:
df <- read.table(
  text = "COL1;    COL2
10;  mary has life
10;  Don mary has life
10;  Britto mary has life
20;  push them
20;  push them fur
30;  yell at this
30;  this is yell at this", 
  sep = ";", header = TRUE, 
  strip.white = TRUE, stringsAsFactors = FALSE)
library(dplyr)
res <- df %>%
  group_by(COL1) %>%
  do(COL2 = {
    first_value <- .$COL2[[1]]
    paste(unlist(Reduce(function(a, b) {
      new_values <- strsplit(b, first_value)[[1]]
      c(a, new_values)
    }, .$COL2)), collapse = ", ")
  })
res$COL2 <- unlist(res$COL2)

