0

An example:

> df1
  c1 c2 c3
1  2  3  8
2  1  6  2
3  2  9  8

I want to get a subset where c1 and c3 values are the same, so in this example I would like the result to be:

  c1 c2 c3
1  2  3  8
3  2  9  8

Is there any straightforward way to do this in R? Thanks.

akrun
  • 874,273
  • 37
  • 540
  • 662
j.du
  • 3
  • 3

1 Answers1

1

We can apply duplicated on the subset of dataset with the columns of interest to find the rows that are all duplicates and use that to subset the rows

df1[duplicated(df1[c('c1','c3')])|duplicated(df1[c('c1','c3')], fromLast=TRUE),]
#  c1 c2 c3
#1  2  3  8
#3  2  9  8
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks, I have tried this and it works. I don't quite understand how the '|' works in this context, but I also tried to add another column c4 and this sentence seem to work as well. – j.du May 03 '17 at 06:24