that might be a really simple question, but we are completely clueless how to answer it.
We have two tables, one column of each table contains the same variables, but in different order. We want to sort the second table in such a way that it matches the order in the first one. I hope it gets a little bit clearer with the following illustration:
EDIT: Instead of X2 = 12 in the right table it should be "X2 = 4.3"
I think this would just be a left_join in dplyr. It isn't apparent from the example data how the first table is ordered so I've assumed it has been set by something else:
The following code joins the tables and drops the Y column from the first table as it's not included in the select(). Also note that the columns become Y.x and Y.y in the join because they are the same name. I've renamed it back to Y but this might not be an issue in your real data:
library(dplyr)
new_output <- t1 %>%
left_join(t2, by = "X") %>%
select(X,
Y = Y.y)
Output:
# A tibble: 3 x 2
X Y
<chr> <dbl>
1 X2 4.3
2 X3 8.2
3 X1 1