简体   繁体   English

R:合并数据框

[英]R: Merging dataframes

I am looking to merge to dataframes, but the manner in which I would like to merge them is a bit uncommon. 我希望合并到数据框,但是我想将它们合并的方式有点不常见。

I will illustrate with an example: 我将举一个例子说明:

 Matrix1 Col1 Col2 Vol VWAP Value ABC 1 2 4 8 ABC 2 3 5 15 DEF 1 8 9 72 DEF 2 8 9 72 Matrix 2 Col1 Col2 Vol VWAP Value ABC 1 4 7 28 ABC 2 5 1 5 HIJ 1 6 6 36 HIJ 2 7 3 21 

I would like to then get the following matrix: 我想得到以下矩阵:

Matrix3 Matrix3

 Col1 Col2 Vol VWAP Value ABC 1 6 6 36 ABC 2 8 2.5 20 DEF 1 8 9 72 DEF 2 8 9 72 HIJ 1 6 6 36 HIJ 2 7 3 21 

In the first two matrices the VWAP column is just the Value column divided by the Vol column. 在前两个矩阵中,VWAP列仅是Value列除以Vol列。 The third matrix combines the first two in the following manner: If the first two Cols are the same, add the Vol and Value cols of the matching rows. 第三个矩阵以以下方式合并前两个:如果前两个列相同,则添加匹配行的Vol和Value列。 If there is no match, just add the unmatched rows to the end of the matrix. 如果没有匹配项,只需将不匹配的行添加到矩阵的末尾。 The VWAP column of Matrix3 is then again just the Value col divided by the Vol col. 然后,Matrix3的VWAP列再次只是Value col除以Vol col。

I tried the following: 我尝试了以下方法:

Matrix3 = merge(Matrix1 ,Matrix2, all = TRUE)  
Matrix3[,4] = Matrix3[,5]/Matrix3[,3]

but for some reason it isn't summing the Vol or the Value columns. 但由于某种原因,它没有将Vol或Value列求和。 I have checked, and the first column is a character, while the rest are either numeric/integer. 我已经检查过了,第一列是一个字符,其余的是数字/整数。

Any ideas? 有任何想法吗?

Thanks 谢谢

Mike 麦克风

If you treat them as data frames, you can append them first using rbind() then use `ddply()' to summarize the Vol, Value, and calculate the V 如果您将它们视为数据帧,则可以先使用rbind()附加它们,然后使用ddply()来总结Vol,Value并计算V

df1<-data.frame(Col1=c("ABC","ABC","DEF","DEF"),
                Col2=c(1,2,1,2),
                Vol=c(2,3,8,8),
                VWAP=c(4,5,9,9),
                Value=c(8,15,72,72))  

df2<-data.frame(Col1=c("ABC","ABC","HIJ","HIJ"),
                Col2=c(1,2,1,2),
                Vol=c(4,5,6,7),
                VWAP=c(7,1,6,3),
                Value=c(28,5,36,21))  

merged=rbind(df1,df2)             # stick the dfs together
require(plyr)                     # library
ddply(merged,
     .(Col1,Col2),
     summarize,
     Vol=sum(Vol),
     VWAP=sum(Value)/sum(Vol),
     Value=sum(Value))

  Col1 Col2 Vol VWAP Value
1  ABC    1   6  6.0    36
2  ABC    2   8  2.5    20
3  DEF    1   8  9.0    72
4  DEF    2   8  9.0    72
5  HIJ    1   6  6.0    36
6  HIJ    2   7  3.0    21

First a comment on notation: Don't call your data.frame Matrix1. 首先对符号进行评论:不要调用data.frame Matrix1。 In R the classes matrix and data.frame are different. R ,类别matrixdata.frame是不同的。

Anyway, the merge command cannot possibly know that it is supposed to add your "Value" and "Vol" columns. 无论如何,merge命令可能无法知道它应该添加“ Value”和“ Vol”列。 You should first merge and then take care of the addition afterwards. 您应该先合并,然后再进行添加。 Here's how you can solve this: 解决方法如下:

m3 <- merge(Matrix1, Matrix2, by=c("Col1", "Col2"), all=TRUE)
# add vol and value
m3[, "Vol"] <- rowSums(m3[, c("Vol.x", "Vol.y")], na.rm=TRUE)
m3[, "Value"] <- rowSums(m3[, c("Value.x", "Value.y")], na.rm=TRUE)
# divide to get vwap
m3[, "VWAP"] <- m3[, "Value"]/m3[, "Vol"]
# extract result
res <- m3[, c("Col1", "Col2", "Vol", "VWAP", "Value")]
res 
##    Col1 Col2 Vol VWAP Value
##  1  ABC    1   6  6.0    36
##  2  ABC    2   8  2.5    20
##  3  DEF    1   8  9.0    72
##  4  DEF    2   8  9.0    72
##  5  HIJ    1   6  6.0    36
##  6  HIJ    2   7  3.0    21

You can do it manullay: 您可以执行以下操作:

id <- mat1$Col1 %in% mat2$Col1 &
    mat1$Col2 %in% mat2$Col2

mat1[id,c('Vol')] <- colSums(rbind(mat1[id,c('Vol')],
                    mat2[id,c('Vol')]))

mat1[id,c('Value')] <- colSums(rbind(mat1[id,c('Value')],
                                     mat2[id,c('Value')]))

m3 <- rbind(mat1,mat2[!id,])

m3[, "VWAP"] <- m3[, "Value"]/m3[, "Vol"]

# Col1 Col2 Vol VWAP Value
# 1   ABC    1   6  6.0    36
# 2   ABC    2   8  2.5    20
# 3   DEF    1   8  9.0    72
# 4   DEF    2   8  9.0    72
# 31  HIJ    1   6  6.0    36
# 41  HIJ    2   7  3.0    21

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

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