PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 29, 2022

[FIXED] How can I join 2 tables with dplyr and keep all columns from the RHS table?

 October 29, 2022     dplyr, left-join, r     No comments   

Issue

Here are 2 data frames:

df1 <- data.frame(ID=c(1,2,3,4))
df1
df2 <- data.frame(ID=c(1,3))
df2

How can I join them to get the following output?:

#  ID.1 ID.2
#     1    1
#     2 
#     3    3
#     4 

Thanks!


Solution

Try dplyr::left_join with keep = TRUE:

> left_join(df1, df2, keep = TRUE, suffix = c('.1', '.2'), by = 'ID')
  ID.1 ID.2
1    1    1
2    2   NA
3    3    3
4    4   NA


Answered By - Dmitry Zotikov
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing