简体   繁体   English

R-合并两个数据帧

[英]R- merging two data frames

I have been having a problem with two data frames that I want to merge. 我一直想合并两个数据帧时遇到问题。 One is larger than the other but they share common column names and rownames. 一个大于另一个,但它们共享公共的列名和行名。

What I would like to do is merge both of them by their specific elements in each row and column. 我想做的是将它们分别通过行和列中的特定元素合并。

For example I have 1 data frame: row.names-US Bond,US Stock,EU Bond,EU Stock,Asia Bond,Asia Stock,col.names-Price Risk,Credit Risk,Market Risk; 例如,我有1个数据框: row.names-US Bond,US Stock,EU Bond,EU Stock,Asia Bond,Asia Stock,col.names-Price Risk,Credit Risk,Market Risk;

The other data frame: row.names-US Bond,US Stock;col.names-Price Risk; 另一个数据框: row.names-US Bond,US Stock;col.names-Price Risk;

Ideally, I would like to merge both data sets by their unique row name and column name. 理想情况下,我想通过它们的唯一行名和列名来合并这两个数据集。

Sample: 样品:

dat1<-matrix(' ',nrow=4,ncol=6)
colnames(dat1)<-c("Value","Percentage","Credit.Risk","Interest.Risk","Interest.Credit.Risk","Total")
rownames(dat1)<-c("Low.Gov.Debt","Low.Corp.Debt","High.Gov.Debt","High.Corp.Debt")
new<-portfolio
rownames(new)<-c("High.Gov.Debt","Low.Gov.Debt")
colnames(new)<-c("Value")

您需要指定您要通过row.names匹配

merge(d1,d2,by="row.names")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM