[R] reshape question

Gabor Grothendieck ggrothendieck at gmail.com
Thu May 3 19:05:06 CEST 2007


Try this:

cn <- names(tab)
nm <- list(Date = grep("Date_", cn, value = TRUE),
   Variable = grep("Variable", cn, value = TRUE),
   Valeur = grep("Valeur", cn, value = TRUE)
)
reshape(tab, direction = "long", varying = nm)


On 5/3/07, GOUACHE David <D.GOUACHE at arvalisinstitutduvegetal.fr> wrote:
> Hello all,
>
> A quick question concerning a behavior of reshape I fail tu understand, probably something obvious but I just can't see it.
>
> With the following data frame further referred to as tab :
>
> Variete;Semis.Date;Date_Mesure.1;Variable_Mesure.1;Valeur_Mesure.1;Date_Mesure.2;Variable_Mesure.2;Valeur_Mesure.2;Date_Mesure.3;Variable_Mesure.3;Valeur_Mesure.3;Date_Mesure.4;Variable_Mesure.4;Valeur_Mesure.4;Date_Mesure.5;Variable_Mesure.5;Valeur_Mesure.5;Date_Mesure.6;Variable_Mesure.6;Valeur_Mesure.6;Date_Mesure.7;Variable_Mesure.7;Valeur_Mesure.7;Date_Mesure.8;Variable_Mesure.8;Valeur_Mesure.8;Date_Mesure.9;Variable_Mesure.9;Valeur_Mesure.9;Date_Mesure.10;Variable_Mesure.10;Valeur_Mesure.10
> A;22/10/2004;38527;Partie aérienne.Verse;3.23;38516;Grain.Humidité;0.71;38523;Grain.Humidité;0.67;38526;Grain.Humidité;0.61;38530;Grain.Humidité;0.56;38532;Grain.Humidité;0.53;38537;Grain.Humidité;0.47;38539;Grain.Humidité;0.45;38544;Grain.Humidité;0.44;38551;Grain.Humidité;0.17
> B;22/10/2004;38527;Partie aérienne.Verse;3.45;38516;Grain.Humidité;0.76;38523;Grain.Humidité;0.68;38526;Grain.Humidité;0.63;38530;Grain.Humidité;0.59;38532;Grain.Humidité;0.54;38537;Grain.Humidité;0.49;38539;Grain.Humidité;0.48;38544;Grain.Humidité;0.46;38551;Grain.Humidité;0.15
>
> I execute the following command :
>
> reshape(tab,direction="long",varying=grep("Mesure",colnames(tab)))
>
> This gives me what I need except for a small glitch : I end up with the column Variable_Mesure containing the numerical values which were contained in the Valeur_Mesure.number columns of the wide data frame and conversely I have in the column Valeurs_Mesure column of my long dataframe the factor levels contained in the Variable_Mesure.number columns of the original wide dataframe.
>
> What is this "inversion" due to ? If it is an error on my part, which I suppose it is, how do I correct this ?
>
> Thanls in advance.
>
> PS - I am running R 2.3.1 on a Windows platform
>
> David Gouache
> Arvalis - Institut du Végétal
> Station de La Minière
> 78280 Guyancourt
> Tel: 01.30.12.96.22 / Port: 06.86.08.94.32
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> 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