Quantcast

Reshaping from long to wide with duplicate idvar and timevar

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Reshaping from long to wide with duplicate idvar and timevar

sbihorel
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

______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Reshaping from long to wide with duplicate idvar and timevar

PIKAL Petr
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:[hidden email]] On Behalf Of Sebastien
> Bihorel
> Sent: Tuesday, March 14, 2017 3:45 AM
> To: [hidden email]
> 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
>
> ______________________________________________
> [hidden email] 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.
______________________________________________
[hidden email] 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.
Loading...