删除包含零的行 [英] Removal of rows containing zero

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

问题描述

我有变量数据集

Year    Age Bag Computer
2008    0   4   4
2008    1   5   3
2008    2   5   12.5
2008    3   5   15
2008    4   5   33
2008    5   5   11
2008    85  5   3.5
2008    .   .   .
2008    108 0   0
2008    109 0   0
2008    110+    0   0

我需要在R中进行子集,以便删除我的数据库中的所有零,并得到这个决赛桌。

I need to subset this in R in order to remove all the zero on my database and get this final table

Year    Age Bag Computer
2008    0   4   4
2008    1   5   3
2008    2   5   12.5
2008    3   5   15
2008    4   5   33
2008    5   5   11
2008    7   5   14.5
2008    8   5       17

推荐答案

如果您想要确定0在列或计算机列中出现的行(假设您的数据框被命名为 dat ,您将使用:

If you want to identify the rows at which the 0 appears in columns Bag or Computer (assuming your dataframe is named dat, you would use:

bad.rows <- which(dat$Bag==0 | dat$Computer==0)

out:

subset(dat, !rownames(dat) %in% bad.rows)

或者你可以跳过这个步骤识别行,只使用子集:

Or you could skip the step of identifying the rows and just use subset:

subset(dat, Bag!=0 & Computer!=0)

注意均衡的否定意味着可能需要切换到使用& ; (或者也许这不是你想要的。)你的描述在这方面有点模糊。可能你只想删除它们,如果两者都为零或删除具有高于特定年龄的所有零的年龄。

Note the negation of the equalities meant one might need to switch to using "&" (or perhaps that was not what you wanted.) Your description was a bit vague on that aspect. Could be you only wanted to remove them if both were zero or remove ages which had all zeros above a particular age.

subset(dat, !(Bag==0 & Computer==0) ) #  ages with any non-zero

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

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