A few possibilities here are my favorites
library(data.table) setDT(df)[, if(+var(number)) .SD, by = from] # from number # 1: 2 1 # 2: 2 2
Basically, for each group we check if there is any variance, if TRUE , then we return the values โโof the group
With base R, I would go with
df[as.logical(with(df, ave(number, from, FUN = var))), ]
Edit : for non-numeric data, you can try the new uniqueN function for the devel data.table version (or use length(unique(number)) > 1 instead
setDT(df)[, if(uniqueN(number) > 1) .SD, by = from]
source share