[R] To build a new Df from 2 Df

Arnaud Michel michel.arnaud at cirad.fr
Tue Oct 14 10:46:27 CEST 2014


Hello

I have 2 df Dem and Rap.
I would want to build all the df (dfnew) by associating these two df 
(Dem and Rap) in the following way :

For each value of Dem$Nom (dfnew$Demandeur), I associate 2 different 
values of Rap$Nom (dfnew$Rapporteur1 and dfnew$Rapporteur2) in such a way

  * for each dfnew$Demandeur, dfnew$Rapporteur1 does not have the same
    value for Departement as Dem$Departement
  * for each dfnew$Demandeur, dfnew$Rapporteur2 does not have the same
    value for Unite as Dem$Unite
  * the value of table(dfnew$Rapporteur1) and the value of
    table(dfnew$Rapporteur2) must be balanced and not too different
    (Accepted differences : 1)

table(dfnew$Rapporteur1)
Rapporteur01 Rapporteur02 Rapporteur03 Rapporteur04 Rapporteur05
            4                   4 4                      4               
   4

Thanks for your help
Michel

  Dem <- structure(list(Nom = c("John", "Jim", "Julie", "Charles", 
"Michel",
"Emma", "Sandra", "Elodie", "Thierry", "Albert", "Jean", "Francois",
"Pierre", "Cyril", "Damien", "Jean-Michel", "Vincent", "Daniel",
"Yvan", "Catherine"), Departement = c("D", "A", "A", "C", "D",
"B", "D", "B", "C", "D", "B", "B", "B", "A", "C", "D", "B", "A",
"D", "D"), Unite = c("Unite8", "Unite4", "Unite4", "Unite7",
"Unite9", "Unite1", "Unite6", "Unite5", "Unite7", "Unite3", "Unite2",
"Unite6", "Unite8", "Unite8", "Unite3", "Unite8", "Unite9", "Unite7",
"Unite9", "Unite5")), .Names = c("Nom", "Departement", "Unite"
), row.names = c(NA, -20L), class = "data.frame")

Rap <- structure(list(Nom = c("Rapporteur01", "Rapporteur02", 
"Rapporteur03",
"Rapporteur04", "Rapporteur05"), Departement = c("C", "D", "C",
"C", "D"), Unite = c("Unite10", "Unite6", "Unite5", "Unite5",
"Unite4")), .Names = c("Nom", "Departement", "Unite"), row.names = c(NA,
-5L), class = "data.frame")

dfnew <- structure(list(Demandeur = structure(c(13L, 12L, 14L, 3L, 15L,
8L, 17L, 7L, 18L, 1L, 10L, 9L, 16L, 4L, 5L, 11L, 19L, 6L, 20L,
2L), .Label = c("Albert", "Catherine", "Charles", "Cyril", "Damien",
"Daniel", "Elodie", "Emma", "Francois", "Jean", "Jean-Michel",
"Jim", "John", "Julie", "Michel", "Pierre", "Sandra", "Thierry",
"Vincent", "Yvan"), class = "factor"), Rapporteur1 = structure(c(3L,
1L, 3L, 5L, 1L, 5L, 1L, 2L, 5L, 4L, 2L, 4L, 2L, 3L, 5L, 4L, 4L,
2L, 3L, 1L), .Label = c("Rapporteur01", "Rapporteur02", "Rapporteur03",
"Rapporteur04", "Rapporteur05"), class = "factor"), Rapporteur2 = 
structure(c(1L,
3L, 4L, 4L, 2L, 4L, 5L, 1L, 2L, 3L, 3L, 3L, 5L, 5L, 1L, 1L, 2L,
5L, 4L, 2L), .Label = c("Rapporteur01", "Rapporteur02", "Rapporteur03",
"Rapporteur04", "Rapporteur05"), class = "factor")), .Names = 
c("Demandeur",
"Rapporteur1", "Rapporteur2"), row.names = c(NA, -20L), class = 
"data.frame")


-- 
Michel ARNAUD
Cirad Montpellier


	[[alternative HTML version deleted]]



More information about the R-help mailing list