Note that:
1. Your solution returns a matrix, not a data frame (or Tibble)
2. Assuming that the order of the entries in the pairs does not matter
(which your solution also assumes and seems reasonable given the OP's
specification), I think that you'll find the following, which returns
the data.frame, is considerably more efficient:
x[!duplicated(cbind(do.call(pmin,x), do.call(pmax,x))),]
For example:
> x <- expand.grid(Source = 1:1000, Target = 1:1000)
> system.time({
y <- apply(x, 1, function(y) return (c(A=min(y), B=max(y))))
unique(t(y))})
user system elapsed
5.075 0.034 5.109
> system.time({
x[!duplicated(cbind(do.call(pmin, x), do.call(pmax, x))), ]
})
user system elapsed
1.340 0.013 1.353
Still more efficient and still returning a data frame is:
w <- x[,2] > x[,1]
x[w,] <- x[w, 2:1]
unique(x)
> system.time({
w <- x[, 2] > x[,1]
x[w, ] <- x[w, 2:1]
unique(x)})
user system elapsed
0.693 0.011 0.703
The efficiency gains are due to vectorization and the use of more
efficient primitives. None of this may matter of course, but it seemed
worth mentioning.
Cheers,
Bert
Bert Gunter
"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Fri, Aug 20, 2021 at 7:13 AM Greg Minshall <minshall at umich.edu>
wrote:>
> Eric,
>
> > x %>% transmute( a=pmin(Source,Target), b=pmax(Source,Target))
%>%
> > unique() %>% rename(Source=a, Target=b)
>
> ah, very nice. i have trouble remembering, e.g., unique().
>
> fwiw, (hopefully) here's a baser version.
> ----
> x = data.frame(Source=rep(1:3,4),
Target=c(rep(1,3),rep(2,3),rep(3,3),rep(4,3)))
>
> y <- apply(x, 1, function(y) return (c(A=min(y), B=max(y))))
> unique(t(y))
> ----
>
> cheers, Greg
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.