合并2个数据帧时保持行名称

时间:2018-04-16 13:25:53

标签: r merge

我有2个数据框,我将它们合并,我想维护第一个数据框df1a

的rownames
df1a = data.frame(Customer = c(1:8),  Product = LETTERS[seq( from = 1, to = 8 )])
df1b = data.frame(Customer = c(11:18), Product = LETTERS[seq( from = 1, to = 8 )])
rownames(df1a)<-LETTERS[seq( from = 1, to = 8 )]
merge(df1a, y = df1b, by = "Product")

1 个答案:

答案 0 :(得分:2)

dplyr - tibble方法可以是,

library(tidyverse)

column_to_rownames(left_join(rownames_to_column(df1a), df1b, by = 'Product'), 'rowname')
  Customer.x Product Customer.y
A          1       A         11
B          2       B         12
C          3       C         13
D          4       D         14
E          5       E         15
F          6       F         16
G          7       G         17
H          8       H         18