0

I'm trying to retain all columns in x, even tho they are missing in y.

This works when I join with y having only a single "group", adding NA entries as expected.

However when I add another "group" to y values are silently dropped.

What am I missing?

Thanks!

library(magrittr)

base <- data.frame(
  "key" = c(1,2,3,4),
  "v_a" = c("asdf","fdsa","qwert","trewq")
)

join1 <- data.frame(
"key" = c(1,2,3),
"group" = c("g_a","g_a","g_a"),
"value" = c(111,222,333)
)

join2 <- data.frame(
  "key" = c(1,2,3,4),
  "group" = c("g_b","g_b","g_b","g_b"),
  "value" = c(444,555,666,777)
)

joincombined <- dplyr::bind_rows(join1, join2)


# joining a single group retains all keys in base
# NAs are correctly assigned for missing values in "group" and "value"

res_singleJoin <- base %>% 
  dplyr::full_join(join1, by = "key")

# joining the combined df drops missing entries "group" and "value"
res_combinedJoin <- base %>% 
  dplyr::full_join(joincombined, by = "key")
eule83
  • 25
  • 7
  • What is your expected outcome? – Quinten Apr 14 '22 at 11:12
  • @eule83 Maybe you want all combinations of `key` and `group` retained? Perhaps something like `base %>% full_join(joincombined, by = "key") %>% complete(key, group)`? Would that give you the desired outcome? – Ben Apr 15 '22 at 02:26
  • Thks! Ben's answer is the desired outcome, here are also 2 different solutions from rstudio.com and reddit: 1. `res_map <- purrr::map_df(list(join1,join2),~dplyr::full_join(base,.x,by = "key"))` 2. `all_key_groups <- base %>% dplyr::select(key) %>% dplyr::full_join(joincombined %>% dplyr::select(group) %>% dplyr::distinct(), by = character()) res_crossJoin <- base %>% dplyr::full_join(all_key_groups, by = "key") %>% dplyr::left_join(joincombined, by = c('key', 'group'))` – eule83 Apr 15 '22 at 09:43

0 Answers0