How can I remove a row with zero values in specific columns?

  • A+
Category:Languages

Assume that my data frame is like this

col1    col2    col3    col4    col5    col6    col7 0   0   0   0   16,75   17,50   18,08 18  24  24  24  19,83   20,47   0,00 18  24  24  24  0,00    21,17   20,73 0   22  0   0   18,67   18,90   21,23 18  24  24  24  0,00    20,42   21,17 18  24  24  24  20,52   21,17   21,92 

I want to remove the rows when columns col5 , col6 and col7 include 0. At the end the shape of data frame should be like this:

col1    col2    col3    col4    col5    col6    col7 0   22  0   0   18,67   18,90   21,23 18  24  24  24  20,52   21,17   21,92 

 


A base R solution:

The sapply finds the records that are not equal to 0, the apply around it tests if the whole row contains only TRUE values and those we select in the data.frame.

df1[apply(sapply(df1[, 5:7], function(x) x != 0), 1, all), ]    col1 col2 col3 col4  col5  col6  col7 1    0    0    0    0 16.75 17.50 18.08 4    0   22    0    0 18.67 18.90 21.23 6   18   24   24   24 20.52 21.17 21.92 

Data (I read your data with dec = "," so all the data was read as a number):

df1 <- structure(list(col1 = c(0L, 18L, 18L, 0L, 18L, 18L), col2 = c(0L,  24L, 24L, 22L, 24L, 24L), col3 = c(0L, 24L, 24L, 0L, 24L, 24L ), col4 = c(0L, 24L, 24L, 0L, 24L, 24L), col5 = c(16.75, 19.83,  0, 18.67, 0, 20.52), col6 = c(17.5, 20.47, 21.17, 18.9, 20.42,  21.17), col7 = c(18.08, 0, 20.73, 21.23, 21.17, 21.92)), class = "data.frame", row.names = c(NA,  -6L)) 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: