add extra information from one data frame to another one

Hi!

I have two data frames. In one data frame, I have 17070 observations, ordered by an ID. One variable is residential municipality. In another data frame, I have some extra information to every residential municipality (2096 observations) like the rurality of the are (rur_type). Now, I would like to add that rur_type to my big data frame.

Here is a sample data:

library(dplyr)

id <- c(1,2,3,4,5,6,7,8,9,10)
r_m <- c(101, 202, 101, 301, 201, 101, 302, 203, 203, 202)
x_data <- data.frame(id, r_m)

r_m <- c(101,201,202,203,301)
rur_type <- c(1,2,2,4,3)
rur_data <- data.frame(r_m, rur_type)

I would like to add the variable rur_type to the x_data as a new column. I tried it with the join function, but then I would have only 5 values observations instead of 10.

Hope, I was clear enough in explaining my problem.

left_join(x_data, rur_data, by = "r_m") should give you exactly what you need.

In a left join, the number of rows of the output is the same as the number of rows of the left input (x_data here).

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Yes, thank you, this is what I was looking for.