本文介绍了如何删除具有任何零值的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题要解决如何删除 R 中具有零值的行.另一方面,我可以使用 na.omit() 删除所有 NA 值或使用 complete.cases() 删除包含 NA 值的行.

I have a problem to solve how to remove rows with a Zero value in R. In others hand, I can use na.omit() to delete all the NA values or use complete.cases() to delete rows that contains NA values.

有没有人知道如何删除 R 中零值的行?

Is there anyone know how to remove rows with a Zero Values in R?

例如:

之前

|    DateTime      | Mac1  | Mac2  | Mac3  | Mac4  |
----------------------------------------------------
| 2011-04-02 06:00 | 20    | 0     | 20    | 20    |
| 2011-04-02 06:05 | 21    | 21    | 21    | 21    |
| 2011-04-02 06:10 | 22    | 22    | 22    | 22    |
| 2011-04-02 06:15 | 23    | 23    | 0     | 23    |
| 2011-04-02 06:20 | 24    | 24    | 24    | 24    |
| 2011-04-02 06:25 | 0     | 25    | 25    | 0     |

之后

|    DateTime      | Mac1  | Mac2  | Mac3  | Mac4  |
----------------------------------------------------
| 2011-04-02 06:05 | 21    | 21    | 21    | 21    |
| 2011-04-02 06:10 | 22    | 22    | 22    | 22    |
| 2011-04-02 06:20 | 24    | 24    | 24    | 24    |

推荐答案

有几种不同的方法可以做到这一点.我更喜欢使用 apply,因为它很容易扩展:

There are a few different ways of doing this. I prefer using apply, since it's easily extendable:

##Generate some data
dd = data.frame(a = 1:4, b= 1:0, c=0:3)

##Go through each row and determine if a value is zero
row_sub = apply(dd, 1, function(row) all(row !=0 ))
##Subset as usual
dd[row_sub,]

这篇关于如何删除具有任何零值的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:38