0

Hi i am trying to extract rows in below table which are duplicate on name,year,month and value are different

  name = c("A","B","B","C","D","E","E","E")
    year =c(2014,2015,2015,2016,2016,2017,2017,2017)
    month =c(1,10,10,5,5,11,11,11)
    value=c(2,30,40,3,1,100,120,140)

    df = data.frame(name,year,month,value)

    name1 = c("B","B","E","E","E")
    year1 =c(2015,2015,2017,2017,2017)
    month1 =c(10,10,11,11,11)
    value1=c(30,40,100,120,140)


out_df = data.frame(name1,year1,month1,value1)

i have tried with below code

out_df =df[duplicated(df), ]

Thanks in advance

san1
  • 455
  • 2
  • 11

1 Answers1

1

One option would be to use duplicated along with fromLast to get all the rows that are duplicated

df[duplicated(df[1:3])|duplicated(df[1:3], fromLast = TRUE),]

NOTE: The last column 'value1' is different for most of the rows. So, we exclude that column to get the logical vector

akrun
  • 874,273
  • 37
  • 540
  • 662