0

Let say I have data like this.

    ConDate    ID    TreN  AriT
   20180424  54294631  1  8030
   20180424  54294631  2  8030
   20180425  25005102  1  8040
   20180425  25005102  2  8045

I want to find data which has same conDate,ID,AriT but different TreN.

In this case first and second row should be selected.

I am not sure how to write query for this kind of situation.

If they meet the requirement, then I want to add extra column next to 'AriT' saying Y for meet the requirement and N for Not meet the requirement.

can some one please help me? Thanks!

Jay
  • 9
  • 1
  • 7

1 Answers1

1

Perhaps something like this using dplyr::group_by and dplyr::filter?

library(dplyr)
df %>%
    group_by(ConDate, ID, AriT) %>%
    filter(n_distinct(TreN) > 1)
## A tibble: 2 x 4
## Groups:   ConDate, ID, AriT [1]
#   ConDate       ID  TreN  AriT
#     <int>    <int> <int> <int>
#1 20180424 54294631     1  8030
#2 20180424 54294631     2  8030

Sample data

df <- read.table(text =
    "   ConDate    ID    TreN  AriT
   20180424  54294631  1  8030
   20180424  54294631  2  8030
   20180425  25005102  1  8040
   20180425  25005102  2  8045", header = T)
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68