data.table version:
library(data.table) sample <- as.data.table(sample) sample[,if(length(unique(Value))==1) .SD ,by=Group] # Group ID Value #1: AA 1 1 #2: AA 2 1 #3: AA 3 1 #4: BB 4 2 #5: BB 5 2 #6: BB 8 2
An alternative using ave if the data is numeric is to check if the variance is 0:
sample[with(sample, ave(Value, Group, FUN=var ))==0,]
An alternative solution that can be faster with big data:
setkey(sample, Group, Value) ans <- sample[unique(sample)[, .N, by=Group][N==1, Group]]
The fact is that calculating unique values ββfor each group can take a lot of time when there are more groups. Instead, we can set the key to data.table , and then take unique values ββby key (which is very fast), and then calculate the total values ββfor each group. Then we need only those where it is 1. Then we can perform a join (which is again very fast). Here is an example of basic data:
require(data.table) set.seed(1L) sample <- data.table(ID=1:1e7, Group = sample(rep(paste0("id", 1:1e5), each=100)), Value = sample(2, 1e7, replace=TRUE, prob=c(0.9, 0.1))) system.time ( ans1 <- sample[,if(length(unique(Value))==1) .SD ,by=Group] )