combination workout in r

Here is my dataframe:

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)

The following explanation illustrates what combination I want to generate and identify that is not possible combination.

Each parents (P1 and P2) has two and can contribute one to their child (Individual).

The parents can have same (for example 1, in the following example) and can contribute one each time.

Thus this becomes a combination game, the following example of combination.

Reciprocals are same (correct): 1 3 is same as 3 1

Question is: creat possible combination and find those that can not be member of combination.

   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

Expected output:

   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

Edits: Second dataset for double check:

   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

Answers


something like

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
> 

Need Your Help

Replacement for DOM3 getUserData/setUserData methods

jquery dom cross-browser deprecated dom3

So we've got some old-school Firefox extension code that uses the DOM3 methods get/setUserData() to pass around data in the DOM. Unfortunately, these have been deprecated in DOM4, so Firefox is pl...

mysql query not running correctly from inside the application

php mysql database codeigniter

I am completely stumped. Here is my php (CodeIgniter) code: