Kimmo,
i'll be curious to see other, maybe more elegant, answers. in the
meantime, this seems to work.
----
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(n=min(y), x=max(y))))
res <- data.frame()
for (n in unique(y["n",])) {
unique(y["x",y["n",]==n])
res <- rbind(res, data.frame(A=c(n), B=unique(y["x",y["n",]==n])))
}
res
----
thanks for the question!
cheers, Greg
Find "undirected" duplicates in a tibble
3 messages · Greg Minshall, Eric Berger, Kimmo Elo
x %>% transmute( a=pmin(Source,Target), b=pmax(Source,Target)) %>% unique() %>% rename(Source=a, Target=b)
On Fri, Aug 20, 2021 at 2:12 PM Greg Minshall <minshall at umich.edu> wrote:
Kimmo,
i'll be curious to see other, maybe more elegant, answers. in the
meantime, this seems to work.
----
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(n=min(y), x=max(y))))
res <- data.frame()
for (n in unique(y["n",])) {
unique(y["x",y["n",]==n])
res <- rbind(res, data.frame(A=c(n), B=unique(y["x",y["n",]==n])))
}
res
----
thanks for the question!
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.
2 days later
Hi! Thank you very much for the fascinating solutions. Nice to learn different approaches. All solutions seem to work with text data as well. @Gabor (who suggested the 'igraph' package): yes, I am familiar with 'igraph' and the solution suggested by you works fine, too. Since I need this kind of "simplification" of my datasets in many analysis outside network analysis, I just wanted to figure out alternatives :-) Best, Kimmo pe, 2021-08-20 kello 16:38 +0300, Eric Berger kirjoitti:
x %>% transmute( a=pmin(Source,Target), b=pmax(Source,Target)) %>% unique() %>% rename(Source=a, Target=b) On Fri, Aug 20, 2021 at 2:12 PM Greg Minshall <minshall at umich.edu> wrote:
Kimmo,
i'll be curious to see other, maybe more elegant, answers. in the
meantime, this seems to work.
----
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(n=min(y), x=max(y))))
res <- data.frame()
for (n in unique(y["n",])) {
unique(y["x",y["n",]==n])
res <- rbind(res, data.frame(A=c(n),
B=unique(y["x",y["n",]==n])))
}
res
----
thanks for the question!
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.