Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combination workout in r

Tags:

r

combinations

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).

enter image description here

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

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

enter image description here

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

enter image description here

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

enter image description here

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
like image 759
jon Avatar asked Dec 06 '25 10:12

jon


1 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
> 
like image 193
shhhhimhuntingrabbits Avatar answered Dec 07 '25 23:12

shhhhimhuntingrabbits



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!