伙计们,
如果您将 2 个数据框 df1 和 df2 连接或合并,我想连接或合并。我的目标很简单,就像创建一个新数据框,其列是 df1 和 df2 的并集。
例子
product=c("p1","p1","p1","p1","p1","p1","p1","p1","p2","p2","p2","p2","p2","p2","p2","p2","p3","p3","p3","p3","p3","p3","p3","p3","p4","p4","p4","p4","p4","p4","p4","p4")
skew=c("b","b","b","b","a","a","a","a","b","b","b","b","a","a","a","a","b","b","b","b","a","a","a","a","b","b","b","b","a","a","a","a")
version=c(0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2,0.1,0.1,0.2,0.2)
color=c("C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2","C1","C2")
price=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32)
df1 = data.frame(product, skew, version)
df2 = data.frame(product, skew, color, price)
我的愿望是得到如下结果。
我尝试了几个选项:
#option 1 with cbind
df <- cbind(df1,df2)
这将返回一个数据框重复列“product”和“skew”。
# Option 2, use data.frame
df <- data.frame(df1,df2)
这给了我几乎想要的东西,除了它有额外的“产品”和“倾斜”列。它们的后缀是“.1”,所以没有重复。
# option 3, use merge which seems to be the way to go
df <- merge(df1,df2)
我认为我在合并时遗漏了一些东西,因为这实际上从所有数据集中创建了一个联合,在提供的 32 个观察值中总共产生了 128 个观察值。我想这就是合并的工作原理。我已经运行了“?合并”并尝试了一些选项,但无法让它吐出我想要的东西。
所以我的问题是:
如上所述从 df1 和 df2 中获取所需数据帧的最佳方法是什么?
提前感谢您的帮助!里亚德。
product skew version color price
1 p1 b 0.1 C1 1
2 p1 b 0.1 C2 2
3 p1 b 0.2 C1 3
4 p1 b 0.2 C2 4
5 p1 a 0.1 C1 5
6 p1 a 0.1 C2 6
7 p1 a 0.2 C1 7
8 p1 a 0.2 C2 8
9 p2 b 0.1 C1 9
10 p2 b 0.1 C2 10
11 p2 b 0.2 C1 11
12 p2 b 0.2 C2 12
13 p2 a 0.1 C1 13
14 p2 a 0.1 C2 14
15 p2 a 0.2 C1 15
16 p2 a 0.2 C2 16
17 p3 b 0.1 C1 17
18 p3 b 0.1 C2 18
19 p3 b 0.2 C1 19
20 p3 b 0.2 C2 20
21 p3 a 0.1 C1 21
22 p3 a 0.1 C2 22
23 p3 a 0.2 C1 23
24 p3 a 0.2 C2 24
25 p4 b 0.1 C1 25
26 p4 b 0.1 C2 26
27 p4 b 0.2 C1 27
28 p4 b 0.2 C2 28
29 p4 a 0.1 C1 29
30 p4 a 0.1 C2 30
31 p4 a 0.2 C1 31
32 p4 a 0.2 C2 32