9

我有的:

我有一个“主”数据框,其中包含以下列:

userid, condition

由于有四个实验条件,我也有四个携带答案信息的数据框,以下列:

userid, condition, answer1, answer2

现在,我想加入这些,因此用户 ID、条件及其对这些条件的答案的所有组合都被合并了。每个条件只应在每行的相应列中具有正确答案。


简短、独立的示例:

master = data.frame(userid=c("foo","foo","foo","foo","bar","bar","bar","bar"), condition=c("A","B","C","D","A","B","C","D"))
cond_a = data.frame(userid=c("foo","bar"), condition="A", answer1=c("1","1"), answer2=c("2","2"))
cond_b = data.frame(userid=c("foo","bar"), condition="B", answer1=c("3","3"), answer2=c("4","4"))
cond_c = data.frame(userid=c("foo","bar"), condition="C", answer1=c("5","5"), answer2=c("6","6"))
cond_d = data.frame(userid=c("foo","bar"), condition="D", answer1=c("7","7"), answer2=c("8","8"))

如何将所有条件合并到主表中,使主表如下所示?

  userid condition answer1 answer2
1    bar         A       1       2
2    bar         B       3       4
3    bar         C       5       6
4    bar         D       7       8
5    foo         A       1       2
6    foo         B       3       4
7    foo         C       5       6
8    foo         D       7       8

我尝试了以下方法:

temp = merge(master, cond_a, all.x=TRUE)

这给了我:

  userid condition answer1 answer2
1    bar         A       1       2
2    bar         B    <NA>    <NA>
3    bar         C    <NA>    <NA>
4    bar         D    <NA>    <NA>
5    foo         A       1       2
6    foo         B    <NA>    <NA>
7    foo         C    <NA>    <NA>
8    foo         D    <NA>    <NA>

但只要我这样做……</p>

merge(temp, cond_b, all.x=TRUE)

条件没有值B。怎么来的?

  userid condition answer1 answer2
1    bar         A       1       2
2    bar         B    <NA>    <NA>
3    bar         C    <NA>    <NA>
4    bar         D    <NA>    <NA>
5    foo         A       1       2
6    foo         B    <NA>    <NA>
7    foo         C    <NA>    <NA>
8    foo         D    <NA>    <NA>
4

3 回答 3

12

您可以使用Reduce()complete.cases()如下:

merged <- Reduce(function(x, y) merge(x, y, all=TRUE), 
                 list(master, cond_a, cond_b, cond_c, cond_d))
merged[complete.cases(merged), ]
#    userid condition answer1 answer2
# 1     bar         A       1       2
# 2     bar         B       3       4
# 4     bar         C       5       6
# 6     bar         D       7       8
# 8     foo         A       1       2
# 9     foo         B       3       4
# 11    foo         C       5       6
# 13    foo         D       7       8

Reduce()可能需要一些时间来适应。您定义您的函数,然后提供一个list对象以重复应用该函数。因此,该声明就像在做:

temp1 <- merge(master, cond_a, all=TRUE)
temp2 <- merge(temp1, cond_b, all=TRUE)
temp3 <- merge(temp2, ....)

或类似的东西:

merge(merge(merge(master, cond_a, all=TRUE), cond_b, all=TRUE), cond_c, all=TRUE)

complete.cases()创建指定列是否“完整”的逻辑向量;此逻辑向量可用于从合并的data.frame.

于 2012-12-08T14:29:41.710 回答
2

正如OP所述,鉴于与主数据框没有明确的关系,一个选项是:

temp <-rbind(cond_a,cond_b,cond_c,cond_d)
temp[order(temp["userid"]),]

或许,如果已知任何关系,可能会有一个不简单的解决方案。

于 2012-12-08T14:51:41.377 回答
1

您可以将此连接表示为SQL语句,然后使用sqldf库执行它。

cond_all = rbind(cond_a, cond_b, cond_c, cond_d)

> sqldf('select p.userid as userid, p.condition as condition, answer1, answer2 from master as p join cond_all as q on p.userid=q.userid and p.condition=q.condition order by userid, condition')
  userid condition answer1 answer2
1    bar         A       1       2
2    bar         B       3       4
3    bar         C       5       6
4    bar         D       7       8
5    foo         A       1       2
6    foo         B       3       4
7    foo         C       5       6
8    foo         D       7       8

您在评论中提到主数据帧具有 cond 数据帧中不存在的额外列。您应该能够修改此 SQL 查询以仍然适用于这种情况。

于 2012-12-09T21:03:40.527 回答