Dear users,

I'm having troubles with reshaping a data.frame from long to wide format.
I copy the output of dput() at the end of the mail because it is quite long.

Each row of the column "Elem" should be transposed to a new column. All variables "Etape", "Ech", "repet", "dilution", "Rincage" define the samples. Meaning that for each unique combination of these variables, I want a single row, and as many columns as elements in "Elem".

So I tried:
reshape(mydata, timevar="Elem", idvar=c("Etape","Ech","repet","dilution","Rincage"), direction="wide", drop=c("ID","Nom_ech"))

The problem is that some columns are not used at all for defining samples. In this case, the row "S1, F10-3, NA, 1, oui" is missing, but I don't understand why.

Can you help me with that? I have no idea what I am doing wrong...

Thanks in advance,
Ivan



mydata <- structure(list(ID = c(543, 544, 545, 546, 547, 548, 549, 550,
551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 11574,
11575, 11576, 11577, 11578, 11579, 11580, 11581, 11582, 11583,
683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 695,
696, 697, 698, 699, 700, 701, 702), Nom_ech = c("Step1 B2", "Step1 B2",
"Step1 B2", "Step1 B2", "Step1 B2", "Step1 B2", "Step1 B2", "Step1 B2",
"Step1 B2", "Step1 B2", "Step1 B2", "Step1 B2", "Step1 B2", "Step1 B2",
"Step1 B2", "Step1 B2", "Step1 B2", "Step1 B2", "Step1 B2", "Step1 B2",
"R1 S1 F1 0-3", "R1 S1 F1 0-3", "R1 S1 F1 0-3", "R1 S1 F1 0-3",
"R1 S1 F1 0-3", "R1 S1 F1 0-3", "R1 S1 F1 0-3", "R1 S1 F1 0-3",
"R1 S1 F1 0-3", "R1 S1 F1 0-3", "S1 F1 0-3 (1) *100", "S1 F1 0-3 (1) *100",
"S1 F1 0-3 (1) *100", "S1 F1 0-3 (1) *100", "S1 F1 0-3 (1) *100",
"S1 F1 0-3 (1) *100", "S1 F1 0-3 (1) *100", "S1 F1 0-3 (1) *100",
"S1 F1 0-3 (1) *100", "S1 F1 0-3 (1) *100", "S1 F1 0-3 (1) *100",
"S1 F1 0-3 (1) *100", "S1 F1 0-3 (1) *100", "S1 F1 0-3 (1) *100",
"S1 F1 0-3 (1) *100", "S1 F1 0-3 (1) *100", "S1 F1 0-3 (1) *100",
"S1 F1 0-3 (1) *100", "S1 F1 0-3 (1) *100", "S1 F1 0-3 (1) *100"
), Etape = c("S1", "S1", "S1", "S1", "S1", "S1", "S1", "S1",
"S1", "S1", "S1", "S1", "S1", "S1", "S1", "S1", "S1", "S1", "S1",
"S1", "S1", "S1", "S1", "S1", "S1", "S1", "S1", "S1", "S1", "S1",
"S1", "S1", "S1", "S1", "S1", "S1", "S1", "S1", "S1", "S1", "S1",
"S1", "S1", "S1", "S1", "S1", "S1", "S1", "S1", "S1"), Ech = c("B2",
"B2", "B2", "B2", "B2", "B2", "B2", "B2", "B2", "B2", "B2", "B2",
"B2", "B2", "B2", "B2", "B2", "B2", "B2", "B2", "F10-3", "F10-3",
"F10-3", "F10-3", "F10-3", "F10-3", "F10-3", "F10-3", "F10-3",
"F10-3", "F10-3", "F10-3", "F10-3", "F10-3", "F10-3", "F10-3",
"F10-3", "F10-3", "F10-3", "F10-3", "F10-3", "F10-3", "F10-3",
"F10-3", "F10-3", "F10-3", "F10-3", "F10-3", "F10-3", "F10-3"
), repet = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1), dilution = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100), Rincage = c("non", "non", "non", "non",
"non", "non", "non", "non", "non", "non", "non", "non", "non",
"non", "non", "non", "non", "non", "non", "non", "oui", "oui",
"oui", "oui", "oui", "oui", "oui", "oui", "oui", "oui", "non",
"non", "non", "non", "non", "non", "non", "non", "non", "non",
"non", "non", "non", "non", "non", "non", "non", "non", "non",
"non"), Elem = c("Al1670", "As1890", "As1937", "Ca1840", "Ca3158",
"Cd2288", "Co2286", "Co2378", "Cr2055", "Cr2835", "Cu2247", "Cu3247",
"Fe2382", "Fe2599", "K_7664", "Mg2795", "Mg2852", "Mn2576", "Mn2605",
"Ni2216", "Al1670", "As1890", "As1937", "Ca1840", "Ca3158", "Cd2288",
"Co2286", "Co2378", "Cr2055", "Cr2835", "Al1670", "As1890", "As1937",
"Ca1840", "Ca3158", "Cd2288", "Co2286", "Co2378", "Cr2055", "Cr2835",
"Cu2247", "Cu3247", "Fe2382", "Fe2599", "K_7664", "Mg2795", "Mg2852",
"Mn2576", "Mn2605", "Ni2216"), Moyenne = c(NA, NA, 3.7455, 596.166666666667,
578.2, 0.1514, NA, 1.87225, NA, 0.3664, 1.859, 1.96766666666667,
NA, NA, 9.29566666666667, 13.08, 12.69, 0.26875, 0.2877, 0.2395,
5732.33333333333, 3.9615, 3.48833333333333, 337.433333333333,
323.533333333333, 0.877166666666667, 0.292466666666667, NA, 1.79566666666667,
NA, 106.326666666667, NA, 2.2755, 291.933333333333, 278.833333333333,
0.819, NA, 3.946, NA, NA, 1.47766666666667, 1.63266666666667,
40.44, 40.2533333333333, 128.9, 50.11, 49.02, 37.4733333333333,
37.775, 0.3764)), .Names = c("ID", "Nom_ech", "Etape", "Ech",
"repet", "dilution", "Rincage", "Elem", "Moyenne"), row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L,
37L, 38L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L,
121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L, 131L
), class = c("tbl_df", "tbl", "data.frame"))

--

Ivan Calandra, PhD
University of Reims Champagne-Ardenne
GEGENAA - EA 3795
CREA - 2 esplanade Roland Garros
51100 Reims, France
+33(0)3 26 77 36 89
ivan.calan...@univ-reims.fr
https://www.researchgate.net/profile/Ivan_Calandra

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to