2014-09-29 9 views
13

Nota: Il titolo di questa interrogazione è stato modificato per renderlo la questione canonica per problemi quando plyr funzioni mascherano le loro controparti dplyr. Il resto della domanda rimane invariato.Perché riepilogare o mutare non funziona quando carico `plyr` dopo` dplyr`?


Supponiamo che io ho i seguenti dati:

dfx <- data.frame(
    group = c(rep('A', 8), rep('B', 15), rep('C', 6)), 
    sex = sample(c("M", "F"), size = 29, replace = TRUE), 
    age = runif(n = 29, min = 18, max = 54) 
) 

Con il buon vecchio plyr posso creare un po 'di tabella che riassume i miei dati con il seguente codice:

require(plyr) 
ddply(dfx, .(group, sex), summarize, 
     mean = round(mean(age), 2), 
     sd = round(sd(age), 2)) 

Il look di uscita in questo modo:

group sex mean sd 
1  A F 49.68 5.68 
2  A M 32.21 6.27 
3  B F 31.87 9.80 
4  B M 37.54 9.73 
5  C F 40.61 15.21 
6  C M 36.33 11.33 

Sto provando a spostare il mio codice su dplyr e sull'operatore %>%. Il mio codice prende DF quindi lo raggruppa per gruppo e sesso e quindi lo riassume. Cioè:

dfx %>% group_by(group, sex) %>% 
    summarise(mean = round(mean(age), 2), sd = round(sd(age), 2)) 

Ma la mia uscita è:

mean sd 
1 35.56 9.92 

Che cosa sto facendo di sbagliato?

Grazie!

risposta

19

Il problema qui è che si sta caricando dplyr prima e poi plyr, quindi la funzione di plyr summarise è mascherare la funzione di dplyr summarise. Quando ciò accade si ottiene questo avvertimento:

require(plyr) 
    Loading required package: plyr 
------------------------------------------------------------------------------------------ 
You have loaded plyr after dplyr - this is likely to cause problems. 
If you need functions from both plyr and dplyr, please load plyr first, then dplyr: 
library(plyr); library(dplyr) 
------------------------------------------------------------------------------------------ 

Attaching package: ‘plyr’ 

The following objects are masked from ‘package:dplyr’: 

    arrange, desc, failwith, id, mutate, summarise, summarize 

Quindi, in modo che il vostro codice funzioni, né staccare plyr detach(package:plyr) o riavviare R e carico plyr prima e poi dplyr (o caricare solo dplyr):

library(dplyr) 
dfx %>% group_by(group, sex) %>% 
    summarise(mean = round(mean(age), 2), sd = round(sd(age), 2)) 
Source: local data frame [6 x 4] 
Groups: group 

    group sex mean sd 
1  A F 41.51 8.24 
2  A M 32.23 11.85 
3  B F 38.79 11.93 
4  B M 31.00 7.92 
5  C F 24.97 7.46 
6  C M 36.17 9.11 

Oppure si può chiamare in modo esplicito dplyr Riassumiamo nel codice, quindi la funzione di destra si chiamerà, non importa quanto si carica i pacchetti:

dfx %>% group_by(group, sex) %>% 
    dplyr::summarise(mean = round(mean(age), 2), sd = round(sd(age), 2)) 
+1

+1 per i vostri pochi secondi :-) – A5C1D2H2I1M1N2O1R2T1

+8

Non capisco perché così poche persone notare che l'avvertimento:/ – hadley

+1

@hadley 'fortune :: fortuna (9)' – Gregor

4

il codice è chiamando 0.123.996,04974 milioniinvece di dplyr::summarise causa l'ordine in cui sono stati caricati "plyr" e "dplyr".

Demo:

library(dplyr) ## I'm guessing this is the order you loaded 
library(plyr) 
dfx %>% group_by(group, sex) %>% 
    summarise(mean = round(mean(age), 2), sd = round(sd(age), 2)) 
# mean sd 
# 1 36.88 9.76 
dfx %>% group_by(group, sex) %>% 
    dplyr::summarise(mean = round(mean(age), 2), sd = round(sd(age), 2)) 
# Source: local data frame [6 x 4] 
# Groups: group 
# 
# group sex mean sd 
# 1  A F 32.17 6.30 
# 2  A M 30.98 7.37 
# 3  B F 38.20 7.67 
# 4  B M 33.12 12.24 
# 5  C F 43.91 10.31 
# 6  C M 47.53 8.25 
Problemi correlati