[英]Remove rows based on reverse condition in columns
I have a large dataset for which this needs to be done but I will describe the problem for a smaller one.我有一个需要完成的大型数据集,但我将描述一个较小的问题。
Var1变量1 | Var2变量2 |
---|---|
A一个 | B乙 |
A一个 | C C |
A一个 | D D |
B乙 | A一个 |
E乙 | F F |
G G | H H |
I want to keep only one of the rows with values "AB" and drop the row with the reverse "BA".我想只保留一个值为“AB”的行,并删除带有反向“BA”的行。 All the other rows should also remain.所有其他行也应该保留。
Thanks in advance.提前致谢。
df[!(df$Var1 == 'B' & df$Var1 == 'A'), ]
With dplyr:使用 dplyr:
dplyr::filter(df, !(Var1 == 'B' & Var1 == 'A'))
Reproducible input data:可重现的输入数据:
df <- data.frame(
Var1 = c('A', 'A', 'A', 'B', 'E', 'G'),
Var2 = c('B', 'C', 'D', 'A', 'F', 'H')
)
if I understand correctly如果我理解正确
df <- df <- data.frame(
Var1 = c('A', 'A', 'A', 'B', 'E', 'G'),
Var2 = c('B', 'C', 'D', 'A', 'F', 'H')
)
fltr <- !duplicated(apply(df, 1, function(x) paste0(sort(x), collapse = "")))
df[fltr, ]
#> Var1 Var2
#> 1 A B
#> 2 A C
#> 3 A D
#> 5 E F
#> 6 G H
Created on 2022-01-11 by the reprex package (v2.0.1)由代表 package (v2.0.1) 于 2022 年 1 月 11 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.