Thank you Petr, It kinda works, but not completely. The problem is that it produces a column for each value ("Moyenne"), and not each element of "Elem". That means I have only one value per column, instead of up to 3. For example, I have 3 columns for Al1670 instead of just one, and each column contains maximum one value (the others being NA). Not sure I am being clear... By the way, I don't understand why my solution did not work; what is wrong there? Thank you again! Ivan -- 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.calandra at univ-reims.fr https://www.researchgate.net/profile/Ivan_Calandra Le 08/09/15 14:04, PIKAL Petr a ?crit :> Hi > > I am not sure if I got it > > library(reshape2) > mm<-melt(mydata, measure.vars="Moyenne") > test <- dcast(mm, Etape+Ech+repet+dilution+Rincage~Elem+value) > > gives me 3 rows but names need some tweaking afterwards. > > nn<-paste("Moyenne", unlist(lapply(strsplit(names(test)[-(1:5)], "_"), "[", 1)), sep=".") > names(test)[-(1:5)]<-nn > > Cheers > Petr > > >> -----Original Message----- >> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ivan >> Calandra >> Sent: Tuesday, September 08, 2015 12:33 PM >> To: R list >> Subject: [R] help with reshape >> >> 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.calandra at univ-reims.fr >> https://www.researchgate.net/profile/Ivan_Calandra >> >> ______________________________________________ >> R-help at 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. > ________________________________ > Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. > Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. > Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. > Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. > > V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: > - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. > - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. > - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. > - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. > > This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. > If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. > If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. > The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. > > In case that this e-mail forms part of business dealings: > - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. > - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. > - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. > - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
Hi I looked into docs to reshape2 and played around a bit and by some magical feature test <- dcast(mm, Etape+Ech+repet+dilution+Rincage~Elem) probably works as you expect. I cannot comment your solution as I use reshape only sparsely. Cheers Petr> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ivan > Calandra > Sent: Tuesday, September 08, 2015 2:18 PM > To: R list > Subject: Re: [R] help with reshape > > Thank you Petr, > > It kinda works, but not completely. The problem is that it produces a > column for each value ("Moyenne"), and not each element of "Elem". That > means I have only one value per column, instead of up to 3. > For example, I have 3 columns for Al1670 instead of just one, and each > column contains maximum one value (the others being NA). > > Not sure I am being clear... > > By the way, I don't understand why my solution did not work; what is > wrong there? > > Thank you again! > Ivan > > -- > 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.calandra at univ-reims.fr > https://www.researchgate.net/profile/Ivan_Calandra > > Le 08/09/15 14:04, PIKAL Petr a ?crit : > > Hi > > > > I am not sure if I got it > > > > library(reshape2) > > mm<-melt(mydata, measure.vars="Moyenne") test <- dcast(mm, > > Etape+Ech+repet+dilution+Rincage~Elem+value) > > > > gives me 3 rows but names need some tweaking afterwards. > > > > nn<-paste("Moyenne", unlist(lapply(strsplit(names(test)[-(1:5)], > "_"), > > "[", 1)), sep=".") names(test)[-(1:5)]<-nn > > > > Cheers > > Petr > > > > > >> -----Original Message----- > >> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ivan > >> Calandra > >> Sent: Tuesday, September 08, 2015 12:33 PM > >> To: R list > >> Subject: [R] help with reshape > >> > >> 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.calandra at univ-reims.fr > >> https://www.researchgate.net/profile/Ivan_Calandra > >> > >> ______________________________________________ > >> R-help at 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. > > ________________________________________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
Thanks Petr, It looks good, but I have to check in more details. Can anyone help me with my original solution using reshape()? I'd like to understand what I did wrong. reshape(mydata, timevar="Elem", idvar=c("Etape","Ech","repet","dilution","Rincage"), direction="wide", drop=c("ID","Nom_ech")) Thank you in advance Ivan -- 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.calandra at univ-reims.fr https://www.researchgate.net/profile/Ivan_Calandra Le 08/09/15 14:45, PIKAL Petr a ?crit :> Hi > > I looked into docs to reshape2 and played around a bit and by some magical feature > > test <- dcast(mm, Etape+Ech+repet+dilution+Rincage~Elem) > > probably works as you expect. > > I cannot comment your solution as I use reshape only sparsely. > > Cheers > Petr > >> -----Original Message----- >> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ivan >> Calandra >> Sent: Tuesday, September 08, 2015 2:18 PM >> To: R list >> Subject: Re: [R] help with reshape >> >> Thank you Petr, >> >> It kinda works, but not completely. The problem is that it produces a >> column for each value ("Moyenne"), and not each element of "Elem". That >> means I have only one value per column, instead of up to 3. >> For example, I have 3 columns for Al1670 instead of just one, and each >> column contains maximum one value (the others being NA). >> >> Not sure I am being clear... >> >> By the way, I don't understand why my solution did not work; what is >> wrong there? >> >> Thank you again! >> Ivan >> >> -- >> 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.calandra at univ-reims.fr >> https://www.researchgate.net/profile/Ivan_Calandra >> >> Le 08/09/15 14:04, PIKAL Petr a ?crit : >>> Hi >>> >>> I am not sure if I got it >>> >>> library(reshape2) >>> mm<-melt(mydata, measure.vars="Moyenne") test <- dcast(mm, >>> Etape+Ech+repet+dilution+Rincage~Elem+value) >>> >>> gives me 3 rows but names need some tweaking afterwards. >>> >>> nn<-paste("Moyenne", unlist(lapply(strsplit(names(test)[-(1:5)], >> "_"), >>> "[", 1)), sep=".") names(test)[-(1:5)]<-nn >>> >>> Cheers >>> Petr >>> >>> >>>> -----Original Message----- >>>> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ivan >>>> Calandra >>>> Sent: Tuesday, September 08, 2015 12:33 PM >>>> To: R list >>>> Subject: [R] help with reshape >>>> >>>> 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.calandra at univ-reims.fr >>>> https://www.researchgate.net/profile/Ivan_Calandra >>>> >>>> ______________________________________________ >>>> R-help at 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. >>> ________________________________ > ________________________________ > Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. > Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. > Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. > Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. > > V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: > - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. > - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. > - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. > - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. > > This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. > If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. > If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. > The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. > > In case that this e-mail forms part of business dealings: > - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. > - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. > - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. > - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.