Hi,
There must be multiple solutions to this, but as we are in the RStudio forums, I think using the dplyr package is the way to go!
dplyr has many useful *_join functions that you can use to merge data frames by matching the values of one or more columns. In this case, full_join is the way to go, as it keeps all the molecules that appear in either of the two data frames.
Here is the code that reproduces your example:
df1 <- data.frame(Molecole = c("A", "B", "D", "E", "G"),
PZ1 = 1, PZ2 = 2, PZ3 = 3)
df2 <- data.frame(Molecole = c("A", "B", "C", "F", "G"),
PZ4 = 4, PZ5 = 5, PZ6 = 6)
merged <- dplyr::full_join(df1, df2) %>%
arrange(Molecole)
full_join merges the tables together, and arrange orders the merged data frame by the column "Molecole" in alphabetical order. Hope this helps!