How to remove columns with same value in R

Just use vapply to go through and check how many unique values there are in each column:

Sample data:

mydf <- data.frame(v1 = 1:4, v2 = 5:8,
                   v3 = 2, v4 = 9:12, v5 = 1)
mydf
##   v1 v2 v3 v4 v5
## 1  1  5  2  9  1
## 2  2  6  2 10  1
## 3  3  7  2 11  1
## 4  4  8  2 12  1

What we will be doing with vapply:

vapply(mydf, function(x) length(unique(x)) > 1, logical(1L))
#    v1    v2    v3    v4    v5 
#  TRUE  TRUE FALSE  TRUE FALSE 

Keep the columns you want:

mydf[vapply(mydf, function(x) length(unique(x)) > 1, logical(1L))]
#   v1 v2 v4
# 1  1  5  9
# 2  2  6 10
# 3  3  7 11
# 4  4  8 12

In case someone tries to do this with dplyr, this yet another way to do it:

library(dplyr)
mydf %>% select(where(~n_distinct(.) > 1))

Tags:

R