简体   繁体   English

R-根据某些列的条件删除行

[英]R- Remove rows based on condition across some columns

I have a data frame like this:我有一个这样的数据框:

在此处输入图像描述

I want to remove rows which have values = 0 in those columns which are "numeric".我想删除那些“数字”列中值 = 0 的行。 I tried some functions but returned to me error o dind't remove anything be cause not the entire row is = 0. Summarizing, i need to remove the rows which are equals to 0 on the colums that have a numeric class( ie from sales month to expected sales,).我尝试了一些函数,但返回给我错误 o 没有删除任何内容,因为不是整行 = 0。总而言之,我需要删除具有数字类的列上等于 0 的行(即来自销售月到预期的销售额,)。 How i could do this???(below attach the result i expect)我怎么能这样做???(下面附上我期望的结果)

在此处输入图像描述

PD: If I could do it with some function that allows me to put the number of the column instead of the name, it would be great! PD:如果我可以用一些 function 来实现,它允许我输入列号而不是名称,那就太好了!

Here a simple solution with lapply .这是一个简单的解决方案lapply

set.seed(5)
df <- data.frame(a=1:10,b=letters[1:10],x=sample(0:5,5,replace = T),y=sample(c(0,10,20,30,40,50),5,replace = T))
df <-df[!unlist(lapply(1:nrow(df), function(i) {
  any(df[i, ] == 0)
})), ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM