【问题标题】:combination workout in rr中的组合锻炼
【发布时间】:2012-06-14 13:23:12
【问题描述】:

这是我的数据框:

name <- c("P1", "P2", "IndA", "IndB", "IndC", "IndD", "IndE", "IndF", "IndG")
    A <- c(1, 3, 1, 2, 2, 5, 5, 1, 4)
    B <- c(2, 4, 3, 4, 2, 2, 6, 2, 2)
    mydf <- data.frame (name, A, B)

下面的解释说明了我想要生成什么组合并识别出不可能的组合。

每个父母(P1 和 P2)都有两个,并且可以为他们的孩子(个人)贡献一个。

父母可以有相同的(例如1,在下面的例子中)并且每次可以贡献一个。

这样就变成了一个组合游戏,下面是组合的例子。

倒数相同(正确):1 3 与 3 1 相同

问题是:创建可能的组合并找出那些不能成为组合成员的组合。

   name <- c("P1", "P2", "IndA", "IndB", "IndC", "IndD", "IndE", "IndF", "IndG")
    A <- c(1, 3, 1, 2, 2, 5, 5, 1, 4)
    B <- c(2, 4, 3, 4, 2, 2, 6, 2, 2)
    mydf <- data.frame (name, A, B)

 name A B
1   P1 1 2
2   P2 3 4
3 IndA 1 3
4 IndB 2 4
5 IndC 2 2
6 IndD 5 2
7 IndE 5 6
8 IndF 1 2
9 IndG 4 2

预期输出:

   name A B     correct 
    1   P1 1 2   NA
    2   P2 3 4   NA
    3 IndA 1 3   TRUE
    4 IndB 2 4   TRUE
    5 IndC 2 2   FALSE
    6 IndD 5 2   FALSE
    7 IndE 5 6   FALSE
    8 IndF 1 2   FALSE
    9 IndG 4 2   TRUE

编辑:用于双重检查的第二个数据集:

   name <- c("P1", "P2", "IndH", "IndI", "IndJ", "IndK")
    A <- c(1, 3, 3, 1, 4, 3)
    B <- c(1, 4, 3, 1, 1, 5)
    mydf2 <- data.frame (name, A, B)
    mydf2
   name A B Correct 
1   P1 1 1    NA
2   P2 3 4    NA
3 IndH 3 3    FALSE
4 IndI 1 1    FALSE
5 IndJ 4 1    TRUE
6 IndK 3 5    FALSE

【问题讨论】:

    标签: r combinations


    【解决方案1】:

    类似

    dum.match<-rbind(expand.grid(c(mydf[1,2:3]),c(mydf[2,2:3])),expand.grid(c(mydf[2,2:3]),c(mydf[1,2:3])))
    newmydf<-cbind(mydf,paste(mydf$A,mydf$B)%in%paste(dum.match$Var1,dum.match$Var2))
    
    > newmydf
      name A B paste(mydf$A, mydf$B) %in% paste(dum.match$Var1, dum.match$Var2)
    1   P1 1 2                                                            FALSE
    2   P2 3 4                                                            FALSE
    3 IndA 1 3                                                             TRUE
    4 IndB 2 4                                                             TRUE
    5 IndC 2 2                                                            FALSE
    6 IndD 5 2                                                            FALSE
    7 IndE 5 6                                                            FALSE
    8 IndF 1 2                                                            FALSE
    9 IndG 4 2                                                             TRUE
    
    dum.match2<-rbind(expand.grid(c(mydf2[1,2:3]),c(mydf2[2,2:3])),expand.grid(c(mydf2[2,2:3]),c(mydf2[1,2:3])))
    newmydf2<-cbind(mydf2,paste(mydf2$A,mydf2$B)%in%paste(dum.match2$Var1,dum.match2$Var2))
    
    > newmydf2
      name A B paste(mydf2$A, mydf2$B) %in% paste(dum.match2$Var1, dum.match2$Var2)
    1   P1 1 1                                                                FALSE
    2   P2 3 4                                                                FALSE
    3 IndH 3 3                                                                FALSE
    4 IndI 1 1                                                                FALSE
    5 IndJ 4 1                                                                 TRUE
    6 IndK 3 5                                                                FALSE
    > 
    

    【讨论】:

    • 谢谢!!解决方案唯一剩下的问题是,每次我需要为每个 P1 和 P2 对创建 dumatchXXX ......我很想尝试创建通用的 dum.match 创建器解决方案,因为我有很多 P1 和 P2 对......自动查找 p1 和 p2 以执行此操作....感谢您的回复
    猜你喜欢
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 2019-11-10
    相关资源
    最近更新 更多