'Remove column with unique length in R [duplicate]

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 1:[1]

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))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GKi