[R] help with reshape

David L Carlson dcarlson at tamu.edu
Tue Sep 8 16:23:16 CEST 2015


I have not followed this thread closely, but this seems to work:

mydata$repet[is.na(mydata$repet)] <- 0
reshape(mydata, timevar="Elem", idvar=c("Etape","Ech", "repet", "dilution","Rincage"),
     direction="wide", drop=c("ID","Nom_ech"))

If this is the expected outcome, the problem is the NA values in repet. I changed them to 0 since you did not have any 0 entries in the data (otherwise you could use 999 or some other value that does not occur in the data). Change them back after running reshape().

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352


----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ivan Calandra
Sent: Tuesday, September 8, 2015 7:56 AM
To: PIKAL Petr; R list
Subject: Re: [R] help with reshape

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.

______________________________________________
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.


More information about the R-help mailing list