r - 当两个特定列总和为零时删除数据帧行
问题描述
structure(list(area_code = c(2, 2, 2, 2, 2, 2), area = c("Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan"), item_code = c(2511, 2805, 2513, 2513, 2514, 2514), item = c("Wheat and products", "Rice (Milled Equivalent)", "Barley and products", "Barley and products", "Maize and products", "Maize and products"), element_code = c(0, 0, 1, 0, 1, 0), element = c("Food", "Food", "Feed", "Food", "Feed", "Food"), Y1961 = c(0, 183, 76, 237, 210, 403), Y2013 = c(0, 422, 360, 89, 200, 76)), row.names = c(NA,-6L),class = c("tbl_df", "tbl", "data.frame"))
如果我的数据框看起来像这样,我希望 R 删除 Y1961 和 Y2013 之和等于 0 的任何行。在此示例中,代码将删除整个第一行。
解决方案
你可以这样做
df = df[-row(subset(df, Y1961 == 0 & Y2013 == 0)),]
推荐阅读
- php - 上传完成时媒体库“HTTP 错误”
- foursquare - 为什么即使使用新令牌,Foursquare API 也会超出配额?
- html - 没有从表单元素中获取属性
- excel - 如何将谷歌电子表格导入excel
- sql - 我在存储 SQL 过程中遇到语法问题
- nativescript - NS Core 和 Typescript 的 Nativescript 文档
- python - python 在 sys.path 中找不到模块
- php - 在 PHP 中使用 foreach 循环解析 Json
- apache - Apache mod_rewrite 后路径中出现虚假但频繁的双斜杠
- amazon-web-services - 为什么我的 AWS DeepLens 无法连接到 WiFi?