[R] help with reshape
PIKAL Petr
petr.pikal at precheza.cz
Tue Sep 8 15:12:00 CEST 2015
Hi
based on your data I got 3 lines with similar results as I got from melt/dcast only the rows are not in the same order.
> version
_
platform i386-w64-mingw32
arch i386
os mingw32
system i386, mingw32
status Under development (unstable)
major 3
minor 3.0
year 2015
month 06
day 15
svn rev 68521
language R
version.string R Under development (unstable) (2015-06-15 r68521)
nickname Unsuffered Consequences
>
> sessionInfo()
R Under development (unstable) (2015-06-15 r68521)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows XP (build 2600) Service Pack 3
locale:
[1] LC_COLLATE=Czech_Czech Republic.1250 LC_CTYPE=Czech_Czech Republic.1250 LC_MONETARY=Czech_Czech Republic.1250
[4] LC_NUMERIC=C LC_TIME=Czech_Czech Republic.1250
attached base packages:
[1] stats datasets utils grDevices graphics methods base
other attached packages:
[1] reshape2_1.4.1 lattice_0.20-31 fun_0.1
loaded via a namespace (and not attached):
[1] Rcpp_0.11.6 digest_0.6.8 MASS_7.3-40 grid_3.3.0 plyr_1.8.3 nlme_3.1-120 gtable_0.1.2 magrittr_1.5
[9] scales_0.2.5 ggplot2_1.0.1 stringi_0.4-1 proto_0.3-10 tools_3.3.0 stringr_1.0.0 munsell_0.4.2 colorspace_1.2-6
>
test2<-reshape(mydata, timevar="Elem", idvar=c("Etape","Ech","repet","dilution","Rincage"),
direction="wide", drop=c("ID","Nom_ech"))
> dput(test2)
structure(list(Etape = c("S1", "S1", "S1"), Ech = c("B2", "F10-3",
"F10-\n 3"), repet = c(NA, 1, 1), dilution = c(1, 100, 100),
Rincage = c("non", "non", "non"), Moyenne.Al1670 = c(NA,
106.326666666667, NA), Moyenne.As1890 = c(NA_real_, NA_real_,
NA_real_), Moyenne.As1937 = c(3.7455, 2.2755, NA), Moyenne.Ca1840 = c(596.166666666667,
291.933333333333, NA), Moyenne.Ca3158 = c(578.2, 278.833333333333,
NA), Moyenne.Cd2288 = c(0.1514, 0.819, NA), Moyenne.Co2286 = c(NA_real_,
NA_real_, NA_real_), Moyenne.Co2378 = c(1.87225, 3.946, NA
), Moyenne.Cr2055 = c(NA_real_, NA_real_, NA_real_), Moyenne.Cr2835 = c(0.3664,
NA, NA), Moyenne.Cu2247 = c(1.859, 1.47766666666667, NA),
Moyenne.Cu3247 = c(1.96766666666667, 1.63266666666667, NA
), Moyenne.Fe2382 = c(NA, 40.44, NA), Moyenne.Fe2599 = c(NA,
40.2533333333333, NA), Moyenne.K_7664 = c(9.29566666666667,
NA, 128.9), Moyenne.Mg2795 = c(13.08, 50.11, NA), Moyenne.Mg2852 = c(12.69,
49.02, NA), Moyenne.Mn2576 = c(0.26875, 37.4733333333333,
NA), Moyenne.Mn2605 = c(0.2877, 37.775, NA), Moyenne.Ni2216 = c(0.2395,
0.3764, NA)), .Names = c("Etape", "Ech", "repet", "dilution",
"Rincage", "Moyenne.Al1670", "Moyenne.As1890", "Moyenne.As1937",
"Moyenne.Ca1840", "Moyenne.Ca3158", "Moyenne.Cd2288", "Moyenne.Co2286",
"Moyenne.Co2378", "Moyenne.Cr2055", "Moyenne.Cr2835", "Moyenne.Cu2247",
"Moyenne.Cu3247", "Moyenne.Fe2382", "Moyenne.Fe2599", "Moyenne.K_7664",
"Moyenne.Mg2795", "Moyenne.Mg2852", "Moyenne.Mn2576", "Moyenne.Mn2605",
"Moyenne.Ni2216"), row.names = c(1L, 112L, 126L), class = c("tbl_df",
"tbl", "data.frame"), reshapeWide = structure(list(v.names = NULL,
timevar = "Elem", idvar = c("Etape", "Ech", "repet", "dilution",
"Rincage"), times = c("Al1670", "As1890", "As1937", "Ca1840",
"Ca3158", "Cd2288", "Co2286", "Co2378", "Cr2055", "Cr2835",
"Cu2247", "Cu3247", "Fe2382", "Fe2599", "K_7664", "Mg2795",
"Mg2852", "Mn2576", "Mn2605", "Ni2216"), varying = structure(c("Moyenne.Al1670",
"Moyenne.As1890", "Moyenne.As1937", "Moyenne.Ca1840", "Moyenne.Ca3158",
"Moyenne.Cd2288", "Moyenne.Co2286", "Moyenne.Co2378", "Moyenne.Cr2055",
"Moyenne.Cr2835", "Moyenne.Cu2247", "Moyenne.Cu3247", "Moyenne.Fe2382",
"Moyenne.Fe2599", "Moyenne.K_7664", "Moyenne.Mg2795", "Moyenne.Mg2852",
"Moyenne.Mn2576", "Moyenne.Mn2605", "Moyenne.Ni2216"), .Dim = c(1L,
20L))), .Names = c("v.names", "timevar", "idvar", "times",
"varying")))
>
dput(test)
structure(list(Etape = c("S1", "S1", "S1", "S1"), Ech = c("B2",
"F10-\n 3", "F10-3", "F10-3"), repet = c(NA, 1, 1, NA), dilution = c(1,
100, 100, 1), Rincage = c("non", "non", "non", "oui"), Al1670 = c(NA,
NA, 106.326666666667, 5732.33333333333), As1890 = c(NA, NA, NA,
3.9615), As1937 = c(3.7455, NA, 2.2755, 3.48833333333333), Ca1840 = c(596.166666666667,
NA, 291.933333333333, 337.433333333333), Ca3158 = c(578.2, NA,
278.833333333333, 323.533333333333), Cd2288 = c(0.1514, NA, 0.819,
0.877166666666667), Co2286 = c(NA, NA, NA, 0.292466666666667),
Co2378 = c(1.87225, NA, 3.946, NA), Cr2055 = c(NA, NA, NA,
1.79566666666667), Cr2835 = c(0.3664, NA, NA, NA), Cu2247 = c(1.859,
NA, 1.47766666666667, NA), Cu3247 = c(1.96766666666667, NA,
1.63266666666667, NA), Fe2382 = c(NA, NA, 40.44, NA), Fe2599 = c(NA,
NA, 40.2533333333333, NA), K_7664 = c(9.29566666666667, 128.9,
NA, NA), Mg2795 = c(13.08, NA, 50.11, NA), Mg2852 = c(12.69,
NA, 49.02, NA), Mn2576 = c(0.26875, NA, 37.4733333333333,
NA), Mn2605 = c(0.2877, NA, 37.775, NA), Ni2216 = c(0.2395,
NA, 0.3764, NA)), .Names = c("Etape", "Ech", "repet", "dilution",
"Rincage", "Al1670", "As1890", "As1937", "Ca1840", "Ca3158",
"Cd2288", "Co2286", "Co2378", "Cr2055", "Cr2835", "Cu2247", "Cu3247",
"Fe2382", "Fe2599", "K_7664", "Mg2795", "Mg2852", "Mn2576", "Mn2605",
"Ni2216"), row.names = c(NA, -4L), class = "data.frame")
>
Cheers
Petr
> -----Original Message-----
> From: Ivan Calandra [mailto:ivan.calandra at univ-reims.fr]
> Sent: Tuesday, September 08, 2015 2:56 PM
> 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"))
> >>>>
> >>>>
________________________________
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.
More information about the R-help
mailing list