连接两个数据框并填充空列

时间:2015-09-30 15:55:00

标签: r data-binding autofill

我需要加入和填充两个数据框。

DF1

X   Mean    sd  SE
1   33.61975308 25.97177258 4.998269963
4   37.21666665 23.65853598 4.553087373
9   27.62490196 23.46366812 3.285570716
5   31.655  13.65466442 2.492989239
6   38.38888889 15.00562282 3.536859218
8   34.22333333 18.45231005 3.551148725
20  32.05768194 19.09932561 2.465712333
211 22.94747986 18.46874547 2.326843217
12  19.97051999 18.11757894 2.282600393
3   18.85689296 18.64166165 2.447770094
14  16.18053903 16.16570095 2.03668688

DF2

N   mean2   sdsub   LCI UCI LCI2    UCI2    LCI3    UCI3
20  32.53997604 20.63952369 29.91928986 35.16066221 30.38002506 34.69992702 30.75555216 34.32439992

第三个数据框需要加入这些并填写DF2

DF3期望

X   Mean    sd  SE  N   mean2   sdsub   LCI UCI LCI2    UCI2    LCI3    UCI3
1   33.61975308 25.97177258 4.998269963 20  32.53997604 20.63952369 29.91928986 35.16066221 30.38002506 34.69992702 30.75555216 34.32439992
4   37.21666665 23.65853598 4.553087373 20  32.53997604 20.63952369 29.91928986 35.16066221 30.38002506 34.69992702 30.75555216 34.32439992
9   27.62490196 23.46366812 3.285570716 20  32.53997604 20.63952369 29.91928986 35.16066221 30.38002506 34.69992702 30.75555216 34.32439992
5   31.655  13.65466442 2.492989239 20  32.53997604 20.63952369 29.91928986 35.16066221 30.38002506 34.69992702 30.75555216 34.32439992
6   38.38888889 15.00562282 3.536859218 20  32.53997604 20.63952369 29.91928986 35.16066221 30.38002506 34.69992702 30.75555216 34.32439992
8   34.22333333 18.45231005 3.551148725 20  32.53997604 20.63952369 29.91928986 35.16066221 30.38002506 34.69992702 30.75555216 34.32439992
20  32.05768194 19.09932561 2.465712333 20  32.53997604 20.63952369 29.91928986 35.16066221 30.38002506 34.69992702 30.75555216 34.32439992
211 22.94747986 18.46874547 2.326843217 20  32.53997604 20.63952369 29.91928986 35.16066221 30.38002506 34.69992702 30.75555216 34.32439992
12  19.97051999 18.11757894 2.282600393 20  32.53997604 20.63952369 29.91928986 35.16066221 30.38002506 34.69992702 30.75555216 34.32439992
3   18.85689296 18.64166165 2.447770094 20  32.53997604 20.63952369 29.91928986 35.16066221 30.38002506 34.69992702 30.75555216 34.32439992
14  16.18053903 16.16570095 2.03668688  20  32.53997604 20.63952369 29.91928986 35.16066221 30.38002506 34.69992702 30.75555216 34.32439992

我已尝试过rbind(),join(),bind_rows()或rbind_all()无济于事。有什么想法吗?

欢呼声

1 个答案:

答案 0 :(得分:1)

合并解决方案。我们创建一个始终具有by参数值的变量。

df1$i <- "A"
df2$i <- "A"
df3 <- merge(df1,df2,by="i")
df3$i <- NULL

df3
     X     Mean       sd       SE  N    mean2    sdsub      LCI      UCI     LCI2     UCI2     LCI3    UCI3
1    1 33.61975 25.97177 4.998270 20 32.53998 20.63952 29.91929 35.16066 30.38003 34.69993 30.75555 34.3244
2    4 37.21667 23.65854 4.553087 20 32.53998 20.63952 29.91929 35.16066 30.38003 34.69993 30.75555 34.3244
3    9 27.62490 23.46367 3.285571 20 32.53998 20.63952 29.91929 35.16066 30.38003 34.69993 30.75555 34.3244
4    5 31.65500 13.65466 2.492989 20 32.53998 20.63952 29.91929 35.16066 30.38003 34.69993 30.75555 34.3244
5    6 38.38889 15.00562 3.536859 20 32.53998 20.63952 29.91929 35.16066 30.38003 34.69993 30.75555 34.3244
6    8 34.22333 18.45231 3.551149 20 32.53998 20.63952 29.91929 35.16066 30.38003 34.69993 30.75555 34.3244
7   20 32.05768 19.09933 2.465712 20 32.53998 20.63952 29.91929 35.16066 30.38003 34.69993 30.75555 34.3244
8  211 22.94748 18.46875 2.326843 20 32.53998 20.63952 29.91929 35.16066 30.38003 34.69993 30.75555 34.3244
9   12 19.97052 18.11758 2.282600 20 32.53998 20.63952 29.91929 35.16066 30.38003 34.69993 30.75555 34.3244
10   3 18.85689 18.64166 2.447770 20 32.53998 20.63952 29.91929 35.16066 30.38003 34.69993 30.75555 34.3244
11  14 16.18054 16.16570 2.036687 20 32.53998 20.63952 29.91929 35.16066 30.38003 34.69993 30.75555 34.3244