Left join versus if-else

When the “to-be-merged” column(s) do not currently exist in the main dataframe, use left join. When the “to-be-merged” info already has a column in the main dataframe, use if-else or case-when.

library(kableExtra)
library(tidyverse)

df1 <- data.frame(
  'id' = c(1, 2, 3, 1, 2, 3, 1, 2, 3),
  'match' = c('a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c')
)


df2 <- data.frame(
  'match' = c('a', 'b'),
  'intracker' = c('yes', 'yes')
)

df1 <- df1 %>% left_join(df2)


df1 %>% kable() %>% kable_styling()
id match intracker
1 a yes
2 b yes
3 c NA
1 a yes
2 b yes
3 c NA
1 a yes
2 b yes
3 c NA
df3 <- data.frame(
  'match' = c('c'),
  'intracker' = c('no')
)

df3 %>% kable() %>% kable_styling()
match intracker
c no

Left join won’t work

df1 <- left_join(df1, df3)

df1 %>% kable() %>% kable_styling()
id match intracker
1 a yes
2 b yes
3 c NA
1 a yes
2 b yes
3 c NA
1 a yes
2 b yes
3 c NA

Mutate case-when works…

df1 <- data.frame(
  'id' = c(1, 2, 3, 1, 2, 3, 1, 2, 3),
  'match' = c('a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c')
)


df2 <- data.frame(
  'match' = c('a', 'b'),
  'intracker' = c('yes', 'yes')
)

df1 <- df1 %>% left_join(df2)


df1 <- df1 %>% mutate(intracker = case_when(
  match == 'c' ~ df3$intracker,
  TRUE ~ intracker
 
))


# or,
# df1 <- df1 %>% mutate(intracker = ifelse(match == 'c', df3$intracker, df1$intracker))


#
# the trick is whether the column already exists


df1 %>% kable() %>% kable_styling()
id match intracker
1 a yes
2 b yes
3 c no
1 a yes
2 b yes
3 c no
1 a yes
2 b yes
3 c no

Bo\(^2\)m =)