r - r中的组合锻炼

标签 r combinations

这是我的数据框:

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)

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

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

enter image description here

parent 可以有相同的(例如 1,在下面的例子中)并且每次可以贡献一个。
enter image description here

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

enter image description here

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

enter image description here

问题是:创建可能的组合并找到那些不能成为组合成员的组合。
   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

enter image description here

预期输出:
   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

最佳答案

就像是

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
> 

关于r - r中的组合锻炼,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11034023/

相关文章:

algorithm - 多重集的 k 组合没有通用解决方案吗?

c# - 不同组按特定顺序的组合

java - 获取给定字母表的所有 4 个字符组合

正则表达式前瞻断言

r - 沿一个面轴解析标签,沿另一个面轴未解析标签

r - 如何选择唯一点

c++ - 与 std::vector 的元素进行组合

r - 将 `round` 或 `sprintf` 函数用于 ggpmisc 和 `dev="tikz 中的回归方程"`

线性回归上的 R 循环

r - R:将数据框的每一行转换为一个列表项