从数据框中删除零值的列 [英] Remove columns with zero values from a dataframe

查看:154
本文介绍了从数据框中删除零值的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个data.frame:

I have a data.frame:

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

它产生一个True / False数据帧,并且:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆