使用merge可以将数据框按照行名合并
df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Toaster", 3), rep("Radio", 3)))
rownames(df1)<-c("a", "b", "c", "d", "e", "f")
df2 = data.frame(CustomerId = c(2, 4, 6), State = c(rep("Alabama", 2), rep("Ohio", 1)))
rownames(df2)<-c("c","e","f")
df1:

df2:

按照列名合并,并输出所有:
df3<-merge(df1, df2, by = "row.names", all = T)
df3

将all改为FALSE会只输出行名相等的行:

网友评论