[R] To transform a dataframe

Arnaud Michel michel.arnaud at cirad.fr
Sat Jun 29 11:26:09 CEST 2013


Hello

I would like to transform the dataframe df1 into df2 (ie copy the data 
from several lines for a men/women to only one line by individu men/women)

dput(df1)
structure(list(Mat = c(934L, 934L, 934L, 935L, 935L, 936L, 936L,
936L, 936L, 937L, 937L, 937L, 937L), Nom = structure(c(2L, 2L,
2L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L), .Label = c("TTTT",
"XXXX", "YYYY", "ZZZZ"), class = "factor"), Sexe = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Masculin", 
class = "factor"),
     Cat = c(6L, 7L, 8L, 7L, 8L, 6L, 7L, 8L, 9L, 3L, 4L, 6L, 7L
     ), Début = structure(c(5L, 7L, 2L, 12L, 8L, 3L, 4L, 10L,
     11L, 13L, 1L, 6L, 9L), .Label = c("01/01/1990", "01/01/2011",
     "01/02/1986", "01/02/1990", "01/07/1986", "01/07/1993", "01/07/1994",
     "01/07/1996", "01/10/2003", "01/11/2002", "01/11/2011", "13/01/1986",
     "23/01/1986"), class = "factor"), Fin = structure(c(2L, 9L,
     10L, 3L, 10L, 5L, 6L, 7L, 10L, 8L, 1L, 4L, 10L), .Label = 
c("30/06/1993",
     "30/06/1994", "30/06/1996", "30/09/2003", "31/01/1990", "31/10/2002",
     "31/10/2011", "31/12/1989", "31/12/2010", "31/12/4712"), class = 
"factor")), .Names = c("Mat",
"Nom", "Sexe", "Cat", "Début", "Fin"), class = "data.frame", row.names = 
c(NA,
-13L))


dput(df2)
structure(list(Mat = 934:937, Nom = structure(c(2L, 3L, 4L, 1L
), .Label = c("TTTT", "XXXX", "YYYY", "ZZZZ"), class = "factor"),
     Sexe = structure(c(1L, 1L, 1L, 1L), .Label = "Masculin", class = 
"factor"),
     Cat4 = c(NA, NA, NA, 4L), Début4 = structure(c(1L, 1L, 1L,
     2L), .Label = c("", "01/01/1990"), class = "factor"), Fin4 = 
structure(c(1L,
     1L, 1L, 2L), .Label = c("", "30/06/1993"), class = "factor"),
     Cat5 = c(NA, NA, NA, NA), Début5 = c(NA, NA, NA, NA), Fin5 = c(NA,
     NA, NA, NA), Cat6 = c(6L, NA, 6L, 6L), Début6 = structure(c(3L,
     1L, 2L, 4L), .Label = c("", "01/02/1986", "01/07/1986", "01/07/1993"
     ), class = "factor"), Fin6 = structure(c(2L, 1L, 4L, 3L), .Label = 
c("",
     "30/06/1994", "30/09/2003", "31/01/1990"), class = "factor"),
     Cat7 = c(7L, 7L, 7L, 7L), Début7 = structure(c(2L, 4L, 1L,
     3L), .Label = c("01/02/1990", "01/07/1994", "01/10/2003",
     "13/01/1986"), class = "factor"), Fin7 = structure(c(3L,
     1L, 2L, 4L), .Label = c("30/06/1996", "31/10/2002", "31/12/2010",
     "31/12/4712"), class = "factor"), Cat8 = c(8L, 8L, 8L, NA
     ), Début8 = structure(c(2L, 3L, 4L, 1L), .Label = c("", "01/01/2011",
     "01/07/1996", "01/11/2002"), class = "factor"), Fin8 = structure(c(3L,
     3L, 2L, 1L), .Label = c("", "31/10/2011", "31/12/4712"), class = 
"factor"),
     Cat9 = c(NA, NA, 9L, NA), Début9 = structure(c(1L, 1L, 2L,
     1L), .Label = c("", "01/11/2011"), class = "factor"), Fin9 = 
structure(c(1L,
     1L, 2L, 1L), .Label = c("", "31/12/4712"), class = "factor")), 
.Names = c("Mat",
"Nom", "Sexe", "Cat4", "Début4", "Fin4", "Cat5", "Début5", "Fin5",
"Cat6", "Début6", "Fin6", "Cat7", "Début7", "Fin7", "Cat8", "Début8",
"Fin8", "Cat9", "Début9", "Fin9"), class = "data.frame", row.names = c(NA,
-4L))


Any idea ?
Thank you

-- 
Michel ARNAUD
Chargé de mission auprès du DRH
DGDRD-Drh - TA 174/04
Av Agropolis 34398 Montpellier cedex 5
tel : 04.67.61.75.38
fax : 04.67.61.57.87
port: 06.47.43.55.31



More information about the R-help mailing list