0

Let's say I have these two datasets

df = data.frame(ID = 1:10, Num = 101:110)
g = data.frame(ID = 1:5, SSE = 1:5)

I want to make the output as follows

   ID Num SSE
1   1 101   1
2   2 102   2
3   3 103   3
4   4 104   4
5   5 105   5
6   6 106  NA
7   7 107  NA
8   8 108  NA
9   9 109  NA
10 10 110  NA

I tried using %in% but didn't work. Thanks.

Anas116
  • 797
  • 2
  • 9

1 Answers1

2

Try dplyr::left_join based on that ID variable.

library(dplyr)

df = data.frame(ID = 1:10, Num = 101:110)
g = data.frame(ID = 1:5, SSE = 1:5)

df %>% 
  left_join(g, by = "ID")

#>    ID Num SSE
#> 1   1 101   1
#> 2   2 102   2
#> 3   3 103   3
#> 4   4 104   4
#> 5   5 105   5
#> 6   6 106  NA
#> 7   7 107  NA
#> 8   8 108  NA
#> 9   9 109  NA
#> 10 10 110  NA

Created on 2023-01-09 with reprex v2.0.2

shafee
  • 15,566
  • 3
  • 19
  • 47