You can use the base R function ave
like this:
datafr[!(ave(datafr$Cond1, datafr$ID, FUN=duplicated)),]
ID Cond1
1 A 10
3 B 20
4 B 30
ave
returns a numeric vector by ID with a 1 if the element of Cond1 is duplicated and a 0 if it is not. the !
performs two roles, first it converts the resulting vector to a logical vector appropriate for subetting. Second it reverses the results, keeping the non-duplicate elements.
In data.table
, you could use a join.
setDT(datafr)[datafr[, !duplicated(Cond1), by=ID]$V1]
ID Cond1
1: A 10
2: B 20
3: B 30
The inner data.frame returns a logical for not duplicated elements by ID and is pulled out into a vector via $V1
. This logical vector is fed to the original data.table to perform the subsetting.
data
datafr <-
structure(list(ID = c("A", "A", "B", "B"), Cond1 = c(10L, 10L,
20L, 30L)), .Names = c("ID", "Cond1"), row.names = c(NA, -4L), class = "data.frame")