[英]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: 当列
col5
, col6
和col7
包含0时,我想删除行。最后,数据框的形状应如下所示:
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
我们可以使用
filter_at
library(tidyverse)
df1 %>%
filter_at(vars(col5, col6, col7), all_vars(. != '0,00'))
A base R solution: 基础R解决方案:
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. sapply找到不等于0的记录,如果整行只包含TRUE值和我们在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): 数据(我用dec =“读取你的数据”,所以所有数据都被读作数字):
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. 一种基本R方法,用于将相关列从数据中进行子集化并检查它们是否为零。 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) 我也用
dec=","
来读取数据(对我来说似乎合乎逻辑)
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=",")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.