0

I would like to introduce data with multiple variables of some observations (df2) into another table with all observations and no variables (df1).

df1 <- data.table(id = c(100:120))
df2 <- data.table(id = c("100","105","115","120"),
                  x = 1:4,
                  y = c(5:8),
                  z = c(8:11))

The outcome would be a table with all ids and data in the maching cells, the rest would be NA. It seems like a simple task but i dont really know how to get it done

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 2
    `left_join(df1, df2)` – Maël Nov 07 '22 at 12:13
  • 2
    Your merge variable `id` must be the same class, so you either need `df1$id` to be strings, or `df2$id` to be integer. You cannot merge on different class variables. Once that is resolved, then this is just `merge(df1, df2, by = "id", all.x = TRUE)` or `dplyr::left_join(df1, df2, by = "id")`. – r2evans Nov 07 '22 at 12:13

0 Answers0