Reputation: 11
Hello I have a vector such as
x<-c(**131144**,**1311605**,1311766,1312289,1312804) in R
And then another data frame like:
v1 , v2
**131144,1283758**
**1283758,19527672**
**1311605,19950311**
198151,37268685
**19950311,35307140**
11281862,11292508
35261079,26296073
625349,37306860
84255273,84259752
I would like to end up with a final vector like this one
x<-c(**19527672**,**19950311**,1311766,1312289,1312804)
Is like to iteratively searching for a value and when a match is found updating it and then keep searching for the updated value until no match found.
Thks in advance.
Upvotes: 1
Views: 77
Reputation: 101247
An option with igraph
g <- graph_from_data_frame(df)
v <- membership(components(g))
tb <- by(names(v), v, function(x) x[degree(g, x, mode = "out") == 0])
m <- unname(v[as.character(x)])
ifelse(is.na(m), x, as.numeric(tb[m]))
gives
[1] 19527672 35307140 1311766 1312289 1312804
where plot(g)
shows
Upvotes: 0
Reputation: 79208
Alittle tweak to the solution found here and we have the following:
relation <- function(vec, dat){
.relation <- function(x){
k = unique(c(dat[dat[, 1] %in% x, 2], x, dat[dat[, 2] %in% x, 1]))
if(setequal(x,k)) tail(k, 2)[1] else Recall(k)
}
y <- unique(vec)
sapply(y, .relation)[match(vec, y)]
}
relation(x, df)
[1] 19527672 35307140 1311766 1312289 1312804
Upvotes: 0