Share
Embed
Les extras sur demande
Réorienter vos données avec reshape Transformer, analyser et résumer vos
données avec plyr
Programmation de base (for, if, while)
Etienne
March 07, 2013
Transcript
14 slides
-
Les extras Réorienter vos données avec reshape Transformer,
analyser et résumer vos données avec plyr Programmation de base (for, if, while) -
ID variable Factor Measured value ID 1 Level 1 Measured
value ID 1 Level 2 Measured value ID 2 Level 1 Measured value ID 2 Level 2 Measured value ID variable Level 1 Level 2 ID 1 Measured value Measured value ID 2 Measured value Measured value Wide Long reshape les ordinateurs aimes les longues données, mais les vôtres sont peut- être large -
Melt: ralonger les données library(reshape)! ! molten.data<-melt(data, id.vars=ls("id.var.1", "id.var.2"),
measure.vars=ls("measure.vars", "measure.vars"), variable_name = "variable")! ! reshape head(molten.iris) head(iris) -
Séparer-Appliquer-Recombiner Split-Apply-Combine Equivalent SQL GROUP BY Pivot
Tables (Excel, SPSS, …) Split Définir des sous-groupes/ parcelles de vos données Apply Faite ce que vous voulez à chacune de ces parcelles Combine Recombiner le résultat individuel de chaque parcelle en un ensemble Journal of Statistical Software 7 2 1 1 2 1,2 Figure 1: The three ways to split up a 2d matrix, labelled above by the dimensions that they slice. Original matrix shown at top left, with dimensions labelled. A single piece under each splitting scheme is colored blue. 3 2 1 1 2 3 1,2 1,3 2,3 1,2,3 Figure 2: The seven ways to split up a 3d array, labelled above by the dimensions that they slice up. Original array shown at top left, with dimensions labelled. Blue indicates a single piece of the output. m*ply() takes a matrix, list-array, or data frame, splits it up by rows and calls the processing function supplying each piece as its parameters. Figure 3 shows how you might use this to draw random numbers from normal distributions with varying parameters. Input: Data frame (d*ply) When operating on a data frame, you usually want to split it up into groups based on com- binations of variables in the data set. For d*ply you specify which variables (or functions of variables) to use. These variables are specified in a special way to highlight that they are Split plyr -
my.function<-function(subset.data){! ! ! ! results<-do.something(subset.data)! return(data.frame(results))}! ! my.function can produce
as many rows as subset.data (transform) or fewer rows than subset.data (summarize) ! returned.results<-ddply(.data=data,! .variable=c("variable1", "variable2”),! ! ! my.function(subset.data))! ! ! Fonctionnement Attention: particularités présentes plyr -
Exemple 1 Calculer la moyenne de chaque mesure pour
chaque espèce en utilisant les données iris reformaté en format long molten.means<-ddply(.data=molten.iris,! !.variables=c("Species", "measure"),! function(subset.data) data.frame(mean=mean(subset.data$value))) plyr -
Exemple 3 Calculer la pente de la largeur sur
la longueur pour chaque organe de la fleur plyr length.on.width.slope<-function(subset.data){ with(subset.data,{ slope.sepal<-lm(Sepal.Width~Sepal.Length)$coefficients[2] slope.petal<-lm(Petal.Width~Petal.Length)$coefficients[2] return(data.frame(slope.sepal=slope.sepal, slope.petal=slope.petal)) }) } iris.slopes<-ddply(.data=iris, .variables="Species", function(x)length.on.width.slope(x)) -
Votre tour changez la fonction sd, length
range=max()-min() utilisez d’autres données simesants, rats, sipoo plyr -
Écrivez votre propre fonction ! ma.fonction<-function(arguments){! ! ! !
résultats<-faite.quelque.chose(arguments)! return(data.frame(résultats))}! ! superficie_ellipse<-function(largeur, longueur){! ! !superficie<-pi*(largeur/2)*(longueur/2)! ! !return(data.frame(superficie=superficie))}! ! with(iris_data, superficie_ellipse(Sepal.Width, Sepal.Length))! ! ! ! ! Manipuler les données -
Programmation For(i in 1:10){function.x(i)} Faite prendre à i
tour à tour les valeurs de 1 à 10 Pour chaque valeur prise par i, appliquer la fonction.x à i for(lettre.selectionnee in LETTERS) {print(lettre.selectionnee)} for(Plant.selectionnee in unique(CO2$Plant)){ fit<-lm(CO2$uptake~CO2$conc) print(summary(fit)} -
Programmation autre fonction de programmation if(Vraie or Faux){commande.x}
exécute la commande x que si la valeur entre parenthèse est un vraie (TRUE) while(Vraie or Faux) {commande.x} répéter l’exécution la commande x tant et aussi longtemps que la valeur entre parenthèse est un vraie (TRUE)