0

I have two tables in R

Table 1 is like this:

ID Value
1 11
2 22

Table 2 is like this:

ID Value New
1 10
2 20
3 30

I have made a new column called New in table 2. I need to bring values from table 1 to table 2 using similar IDs in both tables. My expected table should be like this:

ID Value New
1 10 11
2 20 22
3 30

I need some codes to do this job in RStudio. How can I do this? I appreciate your help.

Babak Kasraei
  • 77
  • 1
  • 6

1 Answers1

0

Using dplyr::full_join and dplyr::rename,

df1 <- data.frame(
  ID = c(1,2),
  Value = c(11,22)
)
df2 <- data.frame(
  ID = c(1,2,3),
  Value = c(10,20,30),
  New = c(NA,NA,NA)
)
df2 %>%
  full_join(df1,by = "ID") %>%
  select(-New) %>%
  rename(Value = Value.x, New = Value.y )

  ID Value New
1  1    10  11
2  2    20  22
3  3    30  NA
Park
  • 14,771
  • 6
  • 10
  • 29