简体   繁体   English

合并具有不相等行且没有匹配列名的数据框 R

[英]Merge dataframes with unequal rows, and no matching column names R

I am trying to take df1 (a summary table), and merge it into df2 (master summary table).我正在尝试将 df1(汇总表)合并到 df2(主汇总表)中。 This is a snapshot of df2, ignore the random 42, just the answer to the ultimate question.这是df2的快照,忽略随机42,只是终极问题的答案。 df2 This is an example of what df1, looks like.这是 df1 的示例。

df1

Lastly, I have a vector called Dates.最后,我有一个名为 Dates 的向量。 This matches the dates that are the column names for df2.这匹配作为 df2 列名的日期。 I am trying to cycle through 20 file, and gather the summary statistics of that file.我正在尝试循环浏览 20 个文件,并收集该文件的摘要统计信息。 I then want to enter that data into df2 to be stored permanently.然后我想将该数据输入 df2 以永久存储。 I only need to enter the Earned column.我只需要输入 Earned 列。 I have tried to use merge but since they do not have shared column names, I am unable to.我曾尝试使用合并,但由于它们没有共享列名,所以我无法使用。 My next attempt was to try this.我的下一个尝试是尝试这个。 But it gave an error, because of unequal row numbers.但它给出了一个错误,因为行号不相等。

df2[,paste(Dates[i])] <- cbind(df2,df1)

Then I thought that maybe if I specified the exact location, it might work.然后我想,如果我指定了确切的位置,它可能会起作用。

df2[1:length(df1$Earned),Dates[i]] <- df1$Earned

But that gave and error "New columns would leave holes after existing columns" So then I thought of trying that again, but with cbind.但这给出了错误“新列会在现有列之后留下孔”所以我想再试一次,但使用 cbind。

df2[1:length(df1$Earned),Dates[i]] <- cbind(df2, df1$Earned)
##This gave an error for differing row numbers
df2 <- cbind(df2[1:length(df1$Earned),Dates[i]],df1$earned)
## This "worked" but it replaced all of df2 with df1$earned, so I basically lost the rest of the master table

Any ideas would be greatly appreciated.任何想法将不胜感激。 Thank you.谢谢你。

Something like this might work:像这样的东西可能会起作用:

df1[df1$TreatyYear %in% df2$TreatyYear, Dates] <- df2$Earned

Example例子

df <- data.frame(matrix(NA,4,4))
df$X1 <- 1:4

df[df$X1 %in% c(1,2),c("X3","X4")] <- c(1,2)

The only solution that I have found so far is to force df1$Earned into a vector.到目前为止,我发现的唯一解决方案是将 df1$Earned 强制转换为向量。 Then append the vector to be the exact length of the df2.然后 append 向量是 df2 的确切长度。 Then I am able to insert the values into df2 by the specific column.然后我可以通过特定列将值插入 df2 。

temp_values <- append(df1$Earned,rep(0,(length(df2$TreatyYear)-length(df1$TreatyYear))),after=length(df1$Earned))
df2[,paste(Dates[i])] <- temp_values

This is kind of a roundabout way to fix it, but not a very pleasant way.这是一种修复它的迂回方式,但不是一种非常愉快的方式。 Any better ideas would be appreciated.任何更好的想法将不胜感激。

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

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