我有两个如下数据框:
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))
我想加入 df2
到df1
,以使Construction
,Industry
和Size
各自具有{ {1}}。
我可以通过使Score
与key
串联的Category
相等然后对每一列进行 left-join 来手动完成此操作,但是我想要一种自动化的方法,这样我就可以轻松添加/删除变量。
这是我想要的格式:(注意:Type
数字不匹配。)
Score
答案 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
上的df2
和c("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