如果类变量重复,如何提取行

时间:2018-02-09 10:40:37

标签: r

您好我正在尝试提取下表中的行,这些行在名称,年份,月份和值上是重复的

  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)

我尝试使用以下代码

out_df =df[duplicated(df), ]

提前致谢

1 个答案:

答案 0 :(得分:1)

一种选择是使用duplicatedfromLast来获取所有重复的行

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

注意:最后一列' value1'大多数行都不同。因此,我们排除该列以获取逻辑向量