R - 连接两个数据帧? [英] R - Concatenate two dataframes?
本文介绍了R - 连接两个数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
a
和 b
: > a
abc
1 -0.2246894 -1.48167912 -1.65099363
2 0.5559320 -0.87898575 -0.15634590
3 1.8469466 -0.01487524 -0.53098215
4 -0.6875051 0.23880967 0.01824621
5 -0.6735163 0.75485292 0.44154092
> b
ac
1 0.4287284 -0.3295925
2 0.5201492 0.3341251
3 -2.6355570 1.7916780
4 -1.3645337 1.3642276
5 -0.4954542 -0.6660001
有没有一种简单的方法连接这些,以便返回一个新的数据框架:
>新
abc
1 -0.2246894 -1.48167912106676 -1.65099363
2 0.5559320 -0.878985746842256 -0.15634590
3 1.8469466 -0.0148752354840942 -0.53098215
4 -0.6875051 0.238809666690982 0.01824621
5 -0.6735163 0.754852923524198 0.44154092
6 0.4287284 NA -0.32959248
7 0.5201492 NA 0.33412510
8 -2.6355570 NA 1.79167801
9 -1.3645337 NA 1.36422764
10 -0.4954542 NA -0.66600006
本质上,我想合并数据框,匹配标题并插入 NA
在数据框中的位置 b
标题缺失。
有一个简单的
解决方案
请参阅现有问题了解更多信息: -
如何在R中将两个data.frames合并在一起,引用查找表
Given two dataframes a
and b
:
> a
a b c
1 -0.2246894 -1.48167912 -1.65099363
2 0.5559320 -0.87898575 -0.15634590
3 1.8469466 -0.01487524 -0.53098215
4 -0.6875051 0.23880967 0.01824621
5 -0.6735163 0.75485292 0.44154092
> b
a c
1 0.4287284 -0.3295925
2 0.5201492 0.3341251
3 -2.6355570 1.7916780
4 -1.3645337 1.3642276
5 -0.4954542 -0.6660001
Is there a simple way to concatenate these so as to return a new data frame of the form:
> new
a b c
1 -0.2246894 -1.48167912106676 -1.65099363
2 0.5559320 -0.878985746842256 -0.15634590
3 1.8469466 -0.0148752354840942 -0.53098215
4 -0.6875051 0.238809666690982 0.01824621
5 -0.6735163 0.754852923524198 0.44154092
6 0.4287284 NA -0.32959248
7 0.5201492 NA 0.33412510
8 -2.6355570 NA 1.79167801
9 -1.3645337 NA 1.36422764
10 -0.4954542 NA -0.66600006
Essentially, I want to merge the dataframes, match the headers and insert NA
in for positions in dataframe b
where the header is missing.
Is there a simple way in which to do this?
解决方案
Refer this existing question for more info:-
How to join data frames in R (inner, outer, left, right)?
How to merge two data.frames together in R, referencing a lookup table
这篇关于R - 连接两个数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文