I have two dataframes with different columns and rows lengths. I want to add two columns in the dataframe A from the dataframe B (columns X and Y) using the "region" column that is present in both dataframes. The figure shows how it looks like and how I would like to get (dataframe C, but can be add to dataframe A as well). I am just showing you the head, but it has a lot of species and the match should be done for all species and the region with x and Y columns. Could someone please help me? enter image description here
>Solution :
You can use dplyr::left_join to add the columns based on region and then use select to select only the columns you need.
library(dplyr)
dataframe_a %>%
left_join(dataframe_b, by = "region") %>%
select(species, region, X, Y)
There are a lot of ways to join two tables. I’m only showing you one, but you can find more material here