Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

Consider two dataframes, df1 and df2.

df1 has columns id, a, b.

df2 has columns id, a, c.

I want to perform a left join such that the combined dataframe has columns id, a, b, c.

combined <- df1 %>% left_join(df2, by="id")

But in the combined dataframe, the columns are id, a.x, b, a.y, c.

How can I do that?

1 Answer

0 votes
by (108k points)

First of all, you have to perform the join by id:

combined <- df1 %>% left_join(df2, by="id")

Then you can rename those with .x and drop those with .y

combined <- combined %>% 

  rename_at(

    vars(ends_with(".x")),

    ~str_replace(., "\\..$","")

  ) %>% 

  select_at(

    vars(-ends_with(".y"))

  )

If you are a beginner and want to know more about R then do check out the R programming tutorial.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Mar 23, 2020 in R Programming by ashely (50.2k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...