Search code examples
rbinarydummy-variable

Removing all binary variables from the data


I have data as follows:

df <- data.frame(A=c(1,2,3), B=c(1,0,1), C=c(0.1, 0.011, 0.3), D=c(0, 0.5, 1))

  A B     C   D
1 1 1 0.100 0.0
2 2 0 0.011 0.5
3 3 1 0.300 1.0

Ho can I remove all binary variables (= B) from this data.frame?


Solution

  • You can use this:

    Filter(function(x) !all(x %in% c(0, 1)), df)
    

    Output:

      A     C   D
    1 1 0.100 0.0
    2 2 0.011 0.5
    3 3 0.300 1.0
    

    Note: you could also add NA to c(0, 1) if needed.