绑定或合并多个变量

时间:2019-03-15 16:04:30

标签: r merge dplyr mutate

我正在寻找一种方法来合并/突变这两个数据帧,并将X $ x_i_plus变量绑定到Y。

 XY <-  merge(X, Y, by.x = c("spd", "gender", "age"), by.y =c("spd", "gender", "age")) 

我认为我可以按spd,性别和年龄进行合并,但这没有用。

我还考虑过使用dpylr突变一个新变量:

test <- test %>% group_by(qnhs, gender, age) %>% mutate(x_i_plus = X$x_i_plus) %>% ungroup() 

这也不起作用。

 > X
       spd   gender   age   x_i_plus
   1    A      1   (0,18]        0
   2    B      1   (0,18]   401986
   3    A      2   (0,18]        0
   4    B      2   (0,18]   380446
   5    A      1  (18,45]   291854
   6    B      1  (18,45]   575662
   7    A      2  (18,45]   245701
   8    B      2  (18,45]   624040
   9    A      1  (45,65]   244692
   10   B      1  (45,65]   227710
   11   A      2  (45,65]   232654
   12   B      2  (45,65]   234826
   13   A      1 (65,101]   203882
   14   B      1 (65,101]   126477
   15   A      2 (65,101]   209999
   16   B      2 (65,101]   102736
> Y
      spd qnhs gender      age m_i_j  
  1    A    A      1   (0,18]     0
  2    B    A      1   (0,18]     0
  3    A    B      1   (0,18]   185
  4    B    B      1   (0,18]  3956
  5    A    A      2   (0,18]     0
  6    B    A      2   (0,18]     0
  7    A    B      2   (0,18]   172
  8    B    B      2   (0,18]  3656
  9    A    A      1  (18,45]  1542
  10   B    A      1  (18,45]  2863
  11   A    B      1  (18,45]   561
  12   B    B      1  (18,45]  3219
  13   A    A      2  (18,45]  1380
  14   B    A      2  (18,45]  2909
  15   A    B      2  (18,45]   637
  16   B    B      2  (18,45]  4117
  17   A    A      1  (45,65]  2060
  18   B    A      1  (45,65]  1921
  19   A    B      1  (45,65]   663
  20   B    B      1  (45,65]   987
  21   A    A      2  (45,65]  2033
  22   B    A      2  (45,65]  2191
  23   A    B      2  (45,65]   559
  24   B    B      2  (45,65]   899
  25   A    A      1 (65,101]  1604
  26   B    A      1 (65,101]  1500
  27   A    B      1 (65,101]    23
  28   B    B      1 (65,101]    27
  29   A    A      2 (65,101]  1712
  30   B    A      2 (65,101]  1112
  31   A    B      2 (65,101]    25
  32   B    B      2 (65,101]    27


> XY
      spd qnhs gender      age m_i_j   x_i_plus
  1    A    A      1   (0,18]     0       0
  2    B    A      1   (0,18]     0    401986
  3    A    B      1   (0,18]   185       0
  4    B    B      1   (0,18]  3956    401986
  5    A    A      2   (0,18]     0       0 
  6    B    A      2   (0,18]     0    380446
  7    A    B      2   (0,18]   172       0
  8    B    B      2   (0,18]  3656    380446
  9    A    A      1  (18,45]  1542    291854
  10   B    A      1  (18,45]  2863    575662
  11   A    B      1  (18,45]   561    291854
  12   B    B      1  (18,45]  3219    575662
  13   A    A      2  (18,45]  1380    245701
  14   B    A      2  (18,45]  2909    624040
  15   A    B      2  (18,45]   637    245701
  16   B    B      2  (18,45]  4117    624040
  17   A    A      1  (45,65]  2060    244692
  18   B    A      1  (45,65]  1921    227710
  19   A    B      1  (45,65]   663    244692
  20   B    B      1  (45,65]   987    227710
  21   A    A      2  (45,65]  2033    232654
  22   B    A      2  (45,65]  2191    234826
  23   A    B      2  (45,65]   559    232654
  24   B    B      2  (45,65]   899    234826
  25   A    A      1 (65,101]  1604    203882
  26   B    A      1 (65,101]  1500    126477
  27   A    B      1 (65,101]    23    203882
  28   B    B      1 (65,101]    27    126477
  29   A    A      2 (65,101]  1712    209999
  30   B    A      2 (65,101]  1112    102736
  31   A    B      2 (65,101]    25    209999
  32   B    B      2 (65,101]    27    102736

任何有关如何执行此操作的想法将不胜感激。

谢谢。

0 个答案:

没有答案