如何使用单个值删除数据框列 [英] How to remove data frame column with a single value
本文介绍了如何使用单个值删除数据框列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我在R中具有以下数据帧:
Let say I have the following data frame in R:
df1 <- data.frame(Item_Name = c("test1","test2","test3"), D_1=c(1,0,1),
D_2=c(1,1,1), D_3=c(11,3,1))
我想创建一个函数来删除无差异的列
(例如在这种情况下,它将删除列 D_2
,因为它只有1个值)
I would like to create a function that would delete columns with no variance
(e.g. in this case, it would remove column D_2
because it has only 1 value)
我知道我可以检查它手动,但实际上我的数据非常大,我想实现自动化。知道吗?
I know that I could check it by hand, but in reality my data is very large and I would like to automate it. Any idea?
推荐答案
过滤器
在这里是有用的功能。我将仅过滤那些唯一值超过1的对象。
Filter
is a useful function here. I will filter only for those where there is more than 1 unique value.
ie
Filter(function(x)(length(unique(x))>1), df1)
## Item_Name D_1 D_3
## 1 test1 1 11
## 2 test2 0 3
## 3 test3 1 1
这篇关于如何使用单个值删除数据框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文