[R] Remove columns from dataframe based on their statistics

J Toll jctoll at gmail.com
Thu May 31 16:20:27 CEST 2012


On Thu, May 31, 2012 at 8:52 AM, J Toll <jctoll at gmail.com> wrote:

> for (i in seq(ncol(df), 1))
>  if (length(unique(df[, i])) == 1) {
>  df[, i] <- NULL
> }

Here's a similar method employing a more functional approach:

df[, apply(df, 2, function(x) length(unique(x)) > 1)]


James



More information about the R-help mailing list