Search code examples
rdata-cleaning

Filtering Columns in R


I have this data frame (train) where I have 2314 variables and I want to drop the columns where the unique length of the column is < 2 and keep the columns where the length is > 1. Maybe I can apply to scale to my function and then run it!

Example:

length(unique(train$makeAcura))
[1] 2

And delete columns with length < 2

length(unique(train$makeAm.General))
[1] 1

Solution

  • You can use Filter to keep the columns where the unique length is > 1.

    Filter(\(x) length(unique(x)) > 1, train)
    #  a c
    #1 1 1
    #2 2 2
    #3 3 1
    

    Data:

    train <- data.frame(a=1:3, b=1, c=c(1,2,1))