简体   繁体   中英

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

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

We can use filter_at

library(tidyverse)
df1 %>% 
   filter_at(vars(col5, col6, col7), all_vars(. != '0,00'))

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))

A base R method that subsets the relevant columns out of the data and checks them for zeros. This does not use any loops.

df[rowSums(df[c("col5", "col6", "col7")] == 0) == 0,]
#   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

I also read the data in with dec="," (seemed logical to me)

df <- read.table(text="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", header=TRUE, dec=",")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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