How can I efficiently extract group-wise constant columns from a data frame? I've included an plyr implementation below to make precise what I'm trying to do, but it's slow. How can I do it as efficiently as possible? (Ideally without splitting the data frame at all).
base <- data.frame(group = 1:1000, a = sample(1000), b = sample(1000)) df <- data.frame( base[rep(seq_len(nrow(base)), length = 1e6), ], c = runif(1e6), d = runif(1e6) ) is.constant <- function(x) length(unique(x)) == 1 constant_cols <- function(x) head(Filter(is.constant, x), 1) system.time(constant <- ddply(df, "group", constant_cols)) # user system elapsed # 20.531 1.670 22.378 stopifnot(identical(names(constant), c("group", "a", "b"))) stopifnot(nrow(constant) == 1000)
In my real use case (deep inside ggplot2) there may be an arbitrary number of constant and non-constant columns. The size of the data in the example is about the right order of magnitude.