Skip to content

Instantly share code, notes, and snippets.

@briandconnelly
Last active August 29, 2015 14:16
Show Gist options
  • Save briandconnelly/f49c0e4d7ae11ed62fb5 to your computer and use it in GitHub Desktop.
Save briandconnelly/f49c0e4d7ae11ed62fb5 to your computer and use it in GitHub Desktop.
Get the names of columns that have >1 unique values in an R data frame
what_varies <- function(d)
{
varies <- function(x) nrow(unique(d[x])) > 1
return(names(d)[unlist(lapply(seq_len(ncol(d)), varies))])
}
@hadley
Copy link

hadley commented Mar 5, 2015

what_varies <- function(d) {
  unique_vals <- function(x) length(unique(x))
  names(d)[vapply(d, unique_vals, integer(1)) > 1]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment