In R, the left_join() function is used to merge two data frames based on a common variable. However, sometimes the common variable has different names in the two data frames. In this case, we can use the by.x and by.y arguments of the left_join() function to specify the names of the common variables in the two data frames.

Here is an example:

Suppose we have two data frames df1 and df2:

df1 <- data.frame(id = c(1, 2, 3, 4),
                  name = c("John", "Mary", "Bob", "Jane"),
                  age = c(25, 30, 35, 40))

df2 <- data.frame(person_id = c(1, 2, 3, 5),
                  city = c("New York", "Boston", "Chicago", "Los Angeles"))

Note that the id variable in df1 corresponds to the person_id variable in df2.

To merge the two data frames based on this variable, we can use the left_join() function as follows:

merged_df <- left_join(df1, df2, by.x = "id", by.y = "person_id")

This will create a new data frame merged_df that contains all the columns from df1 and df2, merged based on the id and person_id variables. The result will look like this:

  id name age      city
1  1 John  25  New York
2  2 Mary  30    Boston
3  3  Bob  35   Chicago
4  4 Jane  40      <NA>

Note that the fourth row has a missing value for city since there is no corresponding person_id in df2

left_join with different varaible in R

原文地址: https://www.cveoy.top/t/topic/hwqd 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录