将长数据框连接到整齐的数据框

时间:2019-05-07 17:14:11

标签: r dataframe join merge

我有两个如下数据框:

df1 <- data.frame(Construction = c("Frame","Frame","Masonry","Fire Resistive","Masonry"),
                 Industry = c("Apartments","Restaurant","Condos","Condos","Condos"),
                 Size = c("[0-3)","[6-9)","[3-6)","[3-6)","9+"))
df2 <- data.frame(Category = c("Construction","Construction","Construction",
                               "Industry","Industry","Industry",
                               "Size","Size","Size","Size"),
                      Type = c("Frame","Masonry","Fire Resistive",
                               "Apartments","Restaurant","Condos",
                                "[0-3)","[3-6)","[6-9)","9+"),
                      Score1 = rnorm(10),
                      Score2 = rnorm(10),
                      Score3 = rnorm(10))

我想加入 df2df1,以使ConstructionIndustrySize各自具有{ {1}}。

我可以通过使Scorekey串联的Category相等然后对每一列进行 left-join 来手动完成此操作,但是我想要一种自动化的方法,这样我就可以轻松添加/删除变量。

这是我想要的格式:(注意:Type数字不匹配。)

Score

1 个答案:

答案 0 :(得分:3)

这里的想法是将namespace WebApplication1 { public partial class WebForm1 : System.Web.UI.Page { int a = 0; int b = 0; static int varCounter = 0; static int varPoints = 0; static string varPuffer; static ImageButton varVorherige; //string varMyMessage = "Leider falsch."; protected void Page_Load(object sender, EventArgs e) { this.Title = "Memory by JanSch"; CreatingButtons(); } private void CreatingButtons() { ImageButton[] buttonArray = new ImageButton[8]; //ButtonArry erstellen (8 Stück) for(int i = 0; i < buttonArray.Length;i++) //Erstellen und hinzufügen der Button { buttonArray[i] = new ImageButton(); buttonArray[i].Width = 75; buttonArray[i].Height = 75; buttonArray[i].ForeColor = System.Drawing.ColorTranslator.FromHtml("#CCCCCC"); buttonArray[i].BackColor = System.Drawing.ColorTranslator.FromHtml("#CCCCCC"); buttonArray[i].Click += new ImageClickEventHandler(buttonArray_Click); this.myPanel.Controls.Add(buttonArray[i]); if (b == 0) { buttonArray[i].AlternateText = a.ToString(); b = 1; } else { buttonArray[i].AlternateText = a.ToString(); a++; b = 0; } } } void buttonArray_Click(object sender, ImageClickEventArgs e) { ImageButton current = sender as ImageButton; this.lblMessage2.Text = "Klick von: " + current.AlternateText; if (varCounter == 0) { varCounter = 1; varPuffer = current.AlternateText; current.ForeColor = System.Drawing.Color.Black; varVorherige = current; } else { if (varPuffer == current.AlternateText) { varPoints++; lblPoints.Text = varPoints.ToString(); varCounter = 0; current.ForeColor = System.Drawing.ColorTranslator.FromHtml("#CCCCCC"); } else { //ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + varMyMessage + "');", true); varCounter = 0; current.ForeColor = System.Drawing.ColorTranslator.FromHtml("#CCCCCC"); varVorherige.ForeColor = System.Drawing.ColorTranslator.FromHtml("#CCCCCC"); } } } } df1上的df2c("Construction","Industry","Size")连接起来,然后构造一个 long 数据帧,该数据帧由那些合并的数据帧组成,我们稍后将其转换为宽格式,以所需的格式获取它。

Type

要注意的一件事是,您将mylist <- lapply(names(df1), function(col){ merge(x = df1, y = df2, by.x = col, by.y = "Type", all.x = TRUE)}) mydf <- do.call(rbind, mylist) df3 <- reshape(mydf, idvar = c("Construction","Industry","Size"), timevar = "Category", direction = "wide") 作为Score Category 列的值,我认为应该是{{ 1}}来匹配df2中的内容以及Size中所暗示的内容。



更新:回答OP的follow-up question

  • 如果df3中有其他列,但df1中没有其他列怎么办?

df1包含另一列,并对其应用相同的方法:

df2

因此,您只需要在df11中指定将df11 <- cbind(df1, a=1:5) mydf <- do.call(rbind, lapply(names(df11[1:3]), function(col){ merge(x = df11, y = df2, by.x = col, by.y = "Type", all.x = TRUE)})) df33 <- reshape(mydf, idvar = names(df11), timevar = "Category", direction = "wide") lapply一起使用的df11的哪一列,并在merge中包括所有df2中的列是否与reshape匹配。



使用df11包的另一种可能性(感谢@akrun提醒我有关df2的信息):

tidyverse