You can use data.table to group by Group
and Event
and only return the group contents (.SD
) if the number of unique COL1 values (uniqueN(COL1)
) is 1.
library(data.table)
setDT(df)
df[, if(uniqueN(COL1) == 1) .SD, by = .(Group, Event)]
# Group Event COL1
# 1: G1 2 SP3
# 2: G1 2 SP3
# 3: G2 6 SP5
# 4: G3 1 SP1
# 5: G4 6 SP1
Data used:
df <- fread('
Group COL1 Event
G1 SP1 1
G1 SP2 1
G1 SP3 2
G1 SP3 2
G2 SP4 3
G2 SP7 3
G2 SP5 6
G3 SP1 1
G4 SP1 6
')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…