2

I would like to understand how I can drop variables from a data frame in R if they are unary, that contains only one value. I sometimes have data frames with thousands of variables, and one of my first steps would be to get rid of those variables (which often is handed over to me from a data warehouse).

I understand that I can drop columns like

drops <- c("x","z")
DF[,!(names(DF) %in% drops)]

as outlined here: Drop data frame columns by name

But I would like some way of searching through all the variables, and dropping unary only.

Community
  • 1
  • 1
Alexander
  • 624
  • 2
  • 7
  • 18

1 Answers1

8

I think this should identify a "nonunary" variable according to your definition:

nonunary <- function(x) length(unique(x))>1

And this should filter the variables in a data frame accordingly:

DF[sapply(DF,nonunary)]
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453