我有一个数据框,例如:
Col1 Col2 Col3
G1 0 1
G2 1 0
G3 2 1
G4 2 0
G5 0 2
G6 3 4
如果以下情况,我想删除行:
在这里我应该得到:
Col1 Col2 Col3
G3 2 1
G6 3 4
您可以使用subset
并包括条件。
subset(df, Col2 != 0 & Col3 != 0 & (Col2 + Col3) != (1- Col2))
#In this case this gives the same answer
#subset(df, Col2 != 0 & Col3 != 0)
# Col1 Col2 Col3
#3 G3 2 1
#6 G6 3 4
或用于 dplyr::filter
library(dplyr)
df %>% filter(Col2 != 0 & Col3 != 0 & (Col2 + Col3) != (1- Col2))
数据
df <- structure(list(Col1 = structure(1:6, .Label = c("G1", "G2", "G3",
"G4", "G5", "G6"), class = "factor"), Col2 = c(0L, 1L, 2L, 2L,
0L, 3L), Col3 = c(1L, 0L, 1L, 0L, 2L, 4L)), class = "data.frame",
row.names = c(NA, -6L))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句