问题
I want to compare two data frames and check if there are duplicated rows. We assume that the order of columns doesn't matter so if df1 looks like that:
V2 V3
71 78
90 13
12 67
56 32
and df2 like that:
V2 V3
89 45
77 88
78 71
90 13
Then the non duplicated rows from both df will be:
12 67
56 32
89 45
77 88
How can I achieve this goal in easy way?
回答1:
Here's a dplyr solution which will probably be pretty quick on larger datasets
df1 <- data_frame( v1 = c(71,90,12,56), v2 = c(78,13,67,32))
df2 <- data_frame( v1 = c(89,77,78,90), v2 = c(45,88,71,13) )
df3 <- bind_rows(df1, df2)
df3 %>%
rowwise() %>%
mutate(key = paste0( min(v1, v2), max(v1, v2))) %>%
group_by(key) %>%
mutate( size = n()) %>%
filter( size == 1)
This solution only works for two grouping variables, to extend it to multiple variables you basically just need to adjust how to manufacture the key.
Edit: I misunderstood the problem as per comments below.
回答2:
You could try
df3 <- rbind(df1, df2)
df4 <- df3
df3[] <- cbind(do.call(pmax, df3), do.call(pmin, df3))
df4[!(duplicated(df3)|duplicated(df3, fromLast=TRUE)),]
# V2 V3
#3 12 67
#4 56 32
#5 89 45
#6 77 88
回答3:
The solution provided below works for your example data. This approach may be inefficient for rather large dataset. Then again, computer time is cheap. :)
df1 <- read.table(text = " V2 V3
71 78
90 13
12 67
56 32", header = TRUE)
df2 <- read.table(text = "V2 V3
89 45
77 88
78 71
90 13", header = TRUE)
throwoutFunction <- function(x, ca) {
find.duplicates <- apply(ca, MARGIN = 1, FUN = function(y, x) y %in% x, x = x)
filter.duplicates <- apply(find.duplicates, MARGIN = 2, all)
if (any(filter.duplicates)) {
return(data.frame(V2 = NA, V3 = NA))
} else {
data.frame(V2 = x[1], V3 = x[2])
}
}
out1 <- do.call("rbind", apply(df1, MARGIN = 1, FUN = throwoutFunction, ca = df2))
out2 <- do.call("rbind", apply(df2, MARGIN = 1, FUN = throwoutFunction, ca = df1))
out <- na.omit(rbind(out1, out2))
rownames(out) <- 1:nrow(out)
out
V2 V3
1 12 67
2 56 32
3 89 45
4 77 88
来源:https://stackoverflow.com/questions/30101321/compare-two-data-frames-column-order-independent-to-get-non-duplicated-rows