[英]Remove columns with factors that has less than 5 observations per level
I have a dataset composed of more than 100 columns and all columns are of type factor.我有一个由 100 多列组成的数据集,所有列都是类型因子。 Ex:
前任:
animal fruit vehicle color
cat orange car blue
dog apple bus green
dog apple car green
dog orange bus green
In my dataset i need to remove all columns with factors thas has less than 5 observations per level.在我的数据集中,我需要删除所有具有每个级别少于 5 个观察值的因子的列。 In this example, if i want to remove all columns with amount of observations per levels less than or equal to
1
, like blue
or cat
, the algorithm will remove the columns animal
and color
.在此示例中,如果我想删除每个级别的观察量小于或等于
1
的所有列,例如blue
或cat
,算法将删除列animal
和color
。 What is the most elegant way to do this?最优雅的方法是什么?
We can use Filter
with table
我们可以在
table
中使用Filter
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")
We can use select_if
from dplyr
我们可以使用
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.