首页 > 解决方案 > Merge two tables with no relation on columns

问题描述

I would like to merge two tables that has no relations on columns.

This are the examples:

Table A:

enter image description here

Table B:

enter image description here

The idea is to merge the tables, creating a row for each row that are in both tables, and assigning 'NA' to the column that we don't have information. This would be the result example:

enter image description here

I have tried with merge() but in this case I just get one row (with the values) instead of two.

The idea is to merge two tables with a lot of columns and a lot of rows, how can I do this?

标签: rrstudio

解决方案


df.1[, colnames(df.2)] <- NA
df.2[, colnames(df.1)[1:2]] <- NA

rbind(df.1, df.2)
  Gender Age Id Group
1      0  27 NA    NA
2     NA  NA  1     0

样本数据

df.1 <- data.frame(Gender = 0, Age = 27)
df.2 <- data.frame(Id = 1, Group = 0)

推荐阅读