I know there are some similar questions. But the answer for them doesn't help in my case or I did understand them wrong.
My example code
set.seed(0)
df <- data.frame(A = seq(20),
B1 = sample(c(T, F), 20, replace=T),
B2 = sample(c(T, F), 20, replace=T))
rownames(df) <- df$A
df <- df[, c('B1', 'B2'), drop = FALSE]
result <- df[df$B1 == FALSE & df$B2 == FALSE, ]
print(result)
Results in this
> result
B1 B2
1 FALSE FALSE
4 FALSE FALSE
10 FALSE FALSE
16 FALSE FALSE
18 FALSE FALSE
So you see 5 cases at the end. I want this 5 cases be removed from the original data.frame df
without loosing the rownames()
or name the rows. So the rows with names 1, 4, 10, 16, 18
should be removed without causing renameing of the others rows.
What I tried (based on other answers):
> library(dplyr)
> anti_join(df, result)
Joining, by = c("B1", "B2")
B1 B2
1 TRUE FALSE
2 TRUE TRUE
3 FALSE TRUE
4 TRUE TRUE
5 FALSE TRUE
6 FALSE TRUE
7 FALSE TRUE
8 TRUE TRUE
9 TRUE TRUE
10 TRUE FALSE
11 FALSE TRUE
12 TRUE TRUE
13 TRUE FALSE
14 FALSE TRUE
15 TRUE FALSE
The rownames are not correct here. They are simply from 1 to 15.
Another try make no sense to me
> setdiff(df, result)
B1 B2
1 TRUE FALSE
2 TRUE TRUE
3 FALSE TRUE
An option with setdiff
would be :
df[setdiff(rownames(df), rownames(result)), ]
# B1 B2
#2 TRUE FALSE
#3 TRUE TRUE
#5 FALSE TRUE
#6 TRUE TRUE
#7 FALSE TRUE
#8 FALSE TRUE
#9 FALSE TRUE
#11 TRUE TRUE
#12 TRUE TRUE
#13 TRUE FALSE
#14 FALSE TRUE
#15 TRUE TRUE
#17 TRUE FALSE
#19 FALSE TRUE
#20 TRUE FALSE
However, a more general approach I would suggest would be to calculate indices only once and use them.
inds <- df$B1 == FALSE & df$B2 == FALSE
result <- df[inds, ]
output <- df[!inds, ]
我们可以%in%
使用!
df[!rownames(df) %in% rownames(result),]
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.