首页 > 解决方案 > 当两个特定列总和为零时删除数据帧行

问题描述

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 的任何行。在此示例中,代码将删除整个第一行。

标签: r

解决方案


你可以这样做

df = df[-row(subset(df, Y1961 == 0 & Y2013 == 0)),]

推荐阅读