r:analisi_descrittiva:statistiche_riassuntive_biv
Indice
Statistiche riassuntive bivariate
Variabili quantitative per gruppi
aggregate
Vedi aggregate
by
La funzione summary()
può essere utilizzata con by()
per avere le statistiche descrittive di variabili quantitative divise per gruppi:
# dati data(SLID, package = "carData") # by con summary by(SLID[1:3], SLID$sex, summary)
## SLID$sex: Female ## wages education age ## Min. : 2.30 Min. : 0.0 Min. :16.0 ## 1st Qu.: 8.06 1st Qu.:10.5 1st Qu.:30.0 ## Median :12.35 Median :12.1 Median :42.0 ## Mean :13.89 Mean :12.4 Mean :44.6 ## 3rd Qu.:17.41 3rd Qu.:14.3 3rd Qu.:58.0 ## Max. :49.92 Max. :20.0 Max. :93.0 ## NA's :1803 NA's :112 ## ----------------------------------------------------- ## SLID$sex: Male ## wages education age ## Min. : 3.00 Min. : 0.0 Min. :16.00 ## 1st Qu.:10.81 1st Qu.:10.0 1st Qu.:30.00 ## Median :16.09 Median :12.0 Median :41.00 ## Mean :17.22 Mean :12.6 Mean :43.31 ## 3rd Qu.:22.00 3rd Qu.:15.0 3rd Qu.:56.00 ## Max. :49.44 Max. :20.0 Max. :95.00 ## NA's :1475 NA's :137
tapply
Vedi tapply e by
tidyverse
Tabelle di contingenza
Il comando summary()
, applicato a due variabili categoriali con by()
produce le due tabelle di frequenza condizionate, e non è di grande utilità.
Applicato ad una tabella di contingenza, produce una descrizione della tabella e il valore del chi-quadrato.
summary(table(SLID$language, SLID$sex))
## Number of cases in table: 7304 ## Number of factors: 2 ## Test for independence of all factors: ## Chisq = 0.24422, df = 2, p-value = 0.8851
Lo stesso si ottiene anche con il summary di xtabs()
:
summary(xtabs(~ SLID$language + SLID$sex))
Script di esempio
E' possibile scaricare ed eseguire lo script dell'esempio:
- Stat_riass_biv.R
data(SLID, package = "carData") # by by(SLID[1:3], SLID$sex, summary) # tapply tapply(SLID$wages, list(SLID$sex), FUN = summary) tapply(SLID$wages, list(SLID$sex), mean, na.rm = T) # tabella di contingenza summary(table(SLID$language, SLID$sex)) summary(xtabs(~ SLID$language + SLID$sex))
r/analisi_descrittiva/statistiche_riassuntive_biv.txt · Ultima modifica: 11/08/2025 14:35 da Agnese Vardanega