从数据框中删除具有零值的列 [英] Remove columns with zero values from a dataframe
本文介绍了从数据框中删除具有零值的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框:
SelectVar
a b c d e f g h i j k l ll m n o p q r
1 Dxa8 Dxa8 0 Dxa8 Dxa8 0 Dxa8 Dxa8 0 0 0 0 0 0 0 0 0 Dxc8 0
2 Dxb8 Dxc8 0 Dxe8 Dxi8 0 tneg tpos 0 0 0 0 0 0 0 0 0 Dxi8 0
我想从数据框中删除两行中具有零值的列,因此它产生如下数据框:
I would like to remove the columns with zero values in both rows from the data frame, so it yields a data frame as below:
SelectVar
a b d e g h q
1 Dxa8 Dxa8 Dxa8 Dxa8 Dxa8 Dxa8 Dxc8
2 Dxb8 Dxc8 Dxe8 Dxi8 tneg tpos Dxi8
尝试过:
SelectVar!=0
产生真/假数据帧,并且:
which yields a True/False dataframe, and:
SelectVar[, colSums(abs(SelectVar)) ! == 0]
产生错误.
如何删除每行中值为零的列?
How could I remove the columns with zero values in each row?
推荐答案
您几乎拥有它.把这两个放在一起:
You almost have it. Put those two together:
SelectVar[, colSums(SelectVar != 0) > 0]
这是有效的,因为因子列被评估为 >= 1 的数字.
This works because the factor columns are evaluated as numerics that are >= 1.
这篇关于从数据框中删除具有零值的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文