1

我有一个由 100 多列组成的数据集,所有列都是类型因子。前任:

          animal               fruit               vehicle              color 
             cat              orange                   car               blue 
             dog               apple                   bus              green 
             dog               apple                   car              green 
             dog              orange                   bus              green

在我的数据集中,我需要删除所有具有每个级别少于 5 个观察值的因子的列。在此示例中,如果我想删除每个级别的观察量小于或等于1blue或的所有列cat,则该算法将删除列animalcolor。最优雅的方法是什么?

4

2 回答 2

1

我们可以Filter使用table

Filter(function(x) !any(table(x) < 2), df1)
#  fruit vehicle
#1 orange     car
#2  apple     bus
#3  apple     car
#4 orange     bus

数据

df1 <- structure(list(animal = structure(c(1L, 2L, 2L, 2L), .Label = c("cat", 
"dog"), class = "factor"), fruit = structure(c(2L, 1L, 1L, 2L
), .Label = c("apple", "orange"), class = "factor"), vehicle = structure(c(2L, 
1L, 2L, 1L), .Label = c("bus", "car"), class = "factor"), color = structure(c(1L, 
2L, 2L, 2L), .Label = c("blue", "green"), class = "factor")),
row.names = c(NA, 
-4L), class = "data.frame")
于 2020-05-14T21:01:07.070 回答
0

我们可以select_if使用dplyr

library(dplyr)
df1 %>% select_if(~all(table(.) > 1))

#   fruit vehicle
#1 orange     car
#2  apple     bus
#3  apple     car
#4 orange     bus
于 2020-05-15T00:39:23.347 回答