Sebastien Bihorel
2017-Mar-14 02:44 UTC
[R] Reshaping from long to wide with duplicate idvar and timevar
Hi, I would like to reshape a data.frame from long to wide format. However, the reshape function does not seem to accept data containing rows with duplicate idvar and timevar. Building upon the ?reshape example: summary(Indometh) wide <- reshape(Indometh, v.names = "conc", idvar = "Subject", timevar = "time", direction = "wide") Indometh2 <- rbind(Indometh, Indometh2[1,]) wide <- reshape(Indometh2, v.names = "conc", idvar = "Subject", timevar = "time", direction = "wide") In the 2nd call, reshape drops the duplicate. In a real world, the process I am working on will handle data with unknown number of duplicates like the one I have manually create above. One "brute force" way to circumvent this would be to pre-process the data, identify the rows with duplicate idvar/timevar combos, and add some suffixes to the idvar variable to make the rows unique. Then I would call reshape, and finally, I would post-process the data to remove the suffixes... This does not seem very elegant. Is there a more efficient way to go about this? Thank you
PIKAL Petr
2017-Mar-14 09:20 UTC
[R] Reshaping from long to wide with duplicate idvar and timevar
Hi You should specify what do you want with duplicated rows. If you wanted them to be averaged you can use dcast from reshape2 package.> dcast(Indometh2,Subject~time, fun.aggregate=mean)Using conc as value column: use value.var to override. Subject 0.25 0.5 0.75 1 1.25 2 3 4 5 6 8 1 1 1.50 0.94 0.78 0.48 0.37 0.19 0.12 0.11 0.08 0.07 0.05 2 4 1.85 1.39 1.02 0.89 0.59 0.40 0.16 0.11 0.10 0.07 0.07 3 2 2.03 1.63 0.71 0.70 0.64 0.36 0.32 0.20 0.25 0.12 0.08 4 5 2.05 1.04 0.81 0.39 0.30 0.23 0.13 0.11 0.08 0.10 0.06 5 6 2.31 1.44 1.03 0.84 0.64 0.42 0.24 0.17 0.13 0.10 0.09 6 3 2.72 1.49 1.16 0.80 0.80 0.39 0.22 0.12 0.11 0.08 0.08 You can see that dcast accepts those duplicated values dcast(Indometh2,Subject~time) Using conc as value column: use value.var to override. Aggregation function missing: defaulting to length Subject 0.25 0.5 0.75 1 1.25 2 3 4 5 6 8 1 1 2 1 1 1 1 1 1 1 1 1 1 2 4 1 1 1 1 1 1 1 1 1 1 1 3 2 1 1 1 1 1 1 1 1 1 1 1 4 5 1 1 1 1 1 1 1 1 1 1 1 5 6 1 1 1 1 1 1 1 1 1 1 1 6 3 1 1 1 1 1 1 1 1 1 1 1 However you probably need to do what you suggested if you want to make those duplicated idvar and timevar to be unique. Cheers Petr> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Sebastien > Bihorel > Sent: Tuesday, March 14, 2017 3:45 AM > To: r-help at r-project.org > Subject: [R] Reshaping from long to wide with duplicate idvar and timevar > > Hi, > > I would like to reshape a data.frame from long to wide format. However, the > reshape function does not seem to accept data containing rows with > duplicate idvar and timevar. Building upon the ?reshape example: > > summary(Indometh) > wide <- reshape(Indometh, v.names = "conc", idvar = "Subject", > timevar = "time", direction = "wide") > > Indometh2 <- rbind(Indometh, Indometh2[1,]) > wide <- reshape(Indometh2, v.names = "conc", idvar = "Subject", > timevar = "time", direction = "wide") > > In the 2nd call, reshape drops the duplicate. In a real world, the process I am > working on will handle data with unknown number of duplicates like the one > I have manually create above. > > One "brute force" way to circumvent this would be to pre-process the data, > identify the rows with duplicate idvar/timevar combos, and add some > suffixes to the idvar variable to make the rows unique. Then I would call > reshape, and finally, I would post-process the data to remove the suffixes... > This does not seem very elegant. > > Is there a more efficient way to go about this? > > Thank you > > ______________________________________________ > 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.