如何删除各列中具有0和Err:502的数据框中的所有行

时间:2019-04-19 06:48:38

标签: r

我有一个数据框ss,它具有26列和100000行。在数据帧中,既有正值也有负值,分别为0和Err:502。

我需要删除数据框中所有具有0和Err:502的行

我在这里提供示例数据框

Dataframe: ss
A    B            C          D      E
C    -1.3456    1.232    23.5453    0
B    56.258     0         -1.00023  234.556
D    43.22435   324.5657 1.00032    345.7
E    Err:502    341.4656 1.9823     32.12
E    0          Err:502  2324       asd
F    0.0004342  234      12         78.31214
F    dfgd       2.313    0.000487   0.12345



ss <- ss[apply(ss[,-1], 1, function(x) !all(x==0)),]
ss[ss != 0, ]



the expected data frame ss should be like as follows 
A         B       C       D      E
D    43.22435   324.5657 1.00032    345.7
F    0.0004342  234      12         78.31214
F    dfgd       2.313    0.000487   0.12345

但是我使用的代码无法正常工作。
预先感谢

3 个答案:

答案 0 :(得分:2)

我们可以使用filter_at

library(dplyr)
ss %>%
    filter_at(-1, all_vars(!.  %in% c(0, "Err:502")))
#   A         B        C         D        E
#1 D  43.22435 324.5657  1.000320    345.7
#2 F 0.0004342      234 12.000000 78.31214
#3 F      dfgd    2.313  0.000487  0.12345

或使用base R

ss[!Reduce(`|`, lapply(ss[-1], `%in%`, c(0, "Err:502"))),]

注意:与apply

相比,这两种方法都是有效的

带有apply的OP的代码可以更正为

ss[apply(ss, 1, function(x) all(! x %in% c(0, "Err:502"))), ]
#  A         B        C         D        E
#3 D  43.22435 324.5657  1.000320    345.7
#6 F 0.0004342      234 12.000000 78.31214
#7 F      dfgd    2.313  0.000487  0.12345

数据

ss <- structure(list(A = c("C", "B", "D", "E", "E", "F", "F"), B = c("-1.3456", 
"56.258", "43.22435", "Err:502", "0", "0.0004342", "dfgd"), C = c("1.232", 
"0", "324.5657", "341.4656", "Err:502", "234", "2.313"), D = c(23.5453, 
-1.00023, 1.00032, 1.9823, 2324, 12, 0.000487), E = c("0", "234.556", 
"345.7", "32.12", "asd", "78.31214", "0.12345")),
 class = "data.frame", row.names = c(NA,  -7L))

答案 1 :(得分:2)

我们可以在底数R中使用rowSums

df[rowSums(df == 0 | df == "Err:502") == 0, ]

# A         B        C         D        E
#3 D  43.22435 324.5657  1.000320    345.7
#6 F 0.0004342      234 12.000000 78.31214
#7 F      dfgd    2.313  0.000487  0.12345

或与apply

df[apply(df, 1, function(x) all(x != 0 & x != "Err:502")), ]

答案 2 :(得分:2)

使用data.table

library(data.table)
df <- as.data.table(df)
out <- df[df[, !Reduce(`|`, lapply(.SD, function(x) x ==0 | x == "Err:502"))]]