I have two tables 1 and 2 and I want to join the tables by a common column. Moreover, I want my data to be sorted so that another column of my first table to be in between two other columns of my second table.
Asked
Active
Viewed 507 times
-2
-
1You can use `dplyr::left_join` to join your tables and square brackets to reorder the results. It's not possible to be more specific because you have not posted any example data or code. Please edit your question to include this if you want more help. – Allan Cameron Aug 20 '20 at 13:23
1 Answers
1
It's difficult to answer properly because you haven't provided any sample data, but something along these lines should work:
library(dplyr)
left_join(table1, table2, by = c("commoncolumn") %>%
select(col2, col1, col3) #reorders columns to order listed
left_join()
will join any rows from table2 to the matching rows of table1 but discard any rows in table2 with no matches. Depending on the behaviour you want, full_join()
(keeps all rows from both tables) or inner_join()
(only keeps rows which are in both tables) might work better for you.

stlba
- 667
- 3
- 13