class: title-slide, center, middle #.title[Introduzione a R] #.subtitle[R and beyond] <img src="data:image/png;base64,#img/arca_logo.svg" width="10%" style="display: block; margin: auto;" /> ###.location[ARCA - @DPSS] ###.author[Filipo Gambarota] --- class: section, center, middle # What to do now? 🤔 --- # Cosa abbiamo visto? - I tipi di oggetti in R - Strutture dati: come creare, accedere, manipolare - Programmazione condizionale e iterativa (`for`, `if else`, `*apply`) - Importare/esportare file Un sacco di cose! 😄. Ma è tutto qui? Nope 🤪 --- # Manipolazione dataframe avanzata con `dplyr` Possiamo utilizzare tutte le conoscenze su come manipolare i dataframe e sulle tipologie di dato con un pacchetto che ormai è diventato lo standard. `dplyr` permette di fare qualsiasi cosa con un dataframe: .pull-left[ <div class="figure" style="text-align: center"> <img src="data:image/png;base64,#img/dplyr.svg" alt="<a href="https://dplyr.tidyverse.org/">https://dplyr.tidyverse.org/</a>" width="50%" /> <p class="caption"><a href="https://dplyr.tidyverse.org/">https://dplyr.tidyverse.org/</a></p> </div> ] .pull-right[ - ordinare righe e colonne - modificare colonne con operazioni complesse - selezionare righe e colonne con operazioni complesse ] --- # Trasformare dataframe con `tidyr` Spesso dobbiamo trasformare i dataframe in diversi formati ad esempio da formato `long` a formato `wide` e viceversa. Questa operazione è molto complessa ma `tiyr` la implementa in modo estremamente semplice. Creiamo un dataframe in formato `long`: .pull-left[ <div class="figure" style="text-align: center"> <img src="data:image/png;base64,#img/tidyr.svg" alt="<a href="https://tidyr.tidyverse.org/">https://tidyr.tidyverse.org/</a>" width="50%" /> <p class="caption"><a href="https://tidyr.tidyverse.org/">https://tidyr.tidyverse.org/</a></p> </div> ] .pull-right[ ```r datlong <- expand.grid( id = 1:10, cond = c("a", "b", "c") ) datlong$y <- rnorm(nrow(datlong)) head(datlong) ``` ``` ## id cond y ## 1 1 a -0.1161931 ## 2 2 a -1.0673929 ## 3 3 a -1.1767938 ## 4 4 a -0.3436329 ## 5 5 a 0.7283757 ## 6 6 a 1.4046698 ``` ] --- # Trasformare dataframe con `tidyr` Adesso possiamo trasformare il dataframe in formato `wide` con una semplice linea di codice con la funzione `pivot_wider()` ```r datwide <- tidyr::pivot_wider(datlong, names_from = cond, values_from = y) head(datlong) ``` ``` ## id cond y ## 1 1 a -0.1161931 ## 2 2 a -1.0673929 ## 3 3 a -1.1767938 ## 4 4 a -0.3436329 ## 5 5 a 0.7283757 ## 6 6 a 1.4046698 ``` --- # Trasformare dataframe con `tidyr` Chiaramente possiamo anche ritornare al formato originale usando la funzione `pivot_longer()`: ```r datlong <- tidyr::pivot_longer(datwide, c(a, b, c), names_to = "cond", values_to = "y") head(datlong) ``` ``` ## [90m# A tibble: 6 × 3[39m ## id cond y ## [3m[90m<int>[39m[23m [3m[90m<chr>[39m[23m [3m[90m<dbl>[39m[23m ## [90m1[39m 1 a -[31m0[39m[31m.[39m[31m116[39m ## [90m2[39m 1 b -[31m1[39m[31m.[39m[31m21[39m ## [90m3[39m 1 c -[31m0[39m[31m.[39m[31m646[39m ## [90m4[39m 2 a -[31m1[39m[31m.[39m[31m0[39m[31m7[39m ## [90m5[39m 2 b 1.11 ## [90m6[39m 2 c -[31m0[39m[31m.[39m[31m904[39m ``` --- # Combinare funzioni con `%>%` o `|>` Quando vi capitera di cercare codice o soluzioni online che riguardano R, il 90% del codice avrà questi strani simboli `%>%` o `|>`. Abbiamo già imparato che questi sono operatori (e quindi funzioni). Questi operatori permettono di concatenare una serie di funzioni in modo molto intuitivo e leggibile. In altri termini `f(x)` è equivalente a `x %>% f()`. Queste si chiamano **pipes**: ```r x <- rnorm(100) mean(x) ``` ``` ## [1] -0.02853021 ``` ```r x %>% mean() ``` ``` ## [1] -0.02853021 ``` ```r x |> mean() ``` ``` ## [1] -0.02853021 ``` --- # Combinare funzioni con `%>%` o `|>` Con funzioni semplici non hanno molto senso ma con *pipeline* più complesse il codice diventa molto leggibile. Se volessimo concatenare una serie di funzioni di `dplyr` (ma funziona con tutte le altre funzioni): ```r iris <- group_by(iris, Species) iris <- summarise(iris, y = mean(Sepal.Width)) iris <- arrange(iris, y) iris <- mutate(iris, z = y + 1) iris <- filter(iris, y > 0) ``` Usando la **pipe**: ```r iris %>% group_by(Species) %>% summarise(y = mean(Sepal.Width)) %>% arrange(y) %>% mutate(z = y + 1) %>% filter(y > 0) ``` --- # Grafici super con `ggplot2` Un pacchetto estremamente potente che si basa su molte delle cose che abbiamo visto e sulla sintassi simil `dplyr` e **pipe-based** è `ggplot2`: .pull-left[ <div class="figure" style="text-align: center"> <img src="data:image/png;base64,#img/ggplot2.svg" alt="<a href="https://ggplot2.tidyverse.org/index.html">https://ggplot2.tidyverse.org/index.html</a>" width="50%" /> <p class="caption"><a href="https://ggplot2.tidyverse.org/index.html">https://ggplot2.tidyverse.org/index.html</a></p> </div> ] .pull-right[ ```r ggplot(iris, aes(Sepal.Length, Petal.Length, color = Species)) + geom_point(size = 3) + theme_minimal(base_size = 20) + theme(legend.position = "bottom") ``` <img src="data:image/png;base64,#5_R_and_beyond_files/figure-html/unnamed-chunk-11-1.png" width="70%" style="display: block; margin: auto;" /> ] --- # Programmazione funzionale/iterativa Il pacchetto `purrr` fornisce un insieme di funzioni che vanno a migliorare la `*apply` family con funzioni extra ed una maggiore robustezza. Se avete capito l'`*apply` family, `purrr` può essere un modo più efficiente di utilizzarle .pull-left[ <div class="figure" style="text-align: center"> <img src="data:image/png;base64,#img/purrr.svg" alt="<a href="https://tidyr.tidyverse.org/">https://tidyr.tidyverse.org/</a>" width="50%" /> <p class="caption"><a href="https://tidyr.tidyverse.org/">https://tidyr.tidyverse.org/</a></p> </div> ] .pull-right[ ```r sapply(mtcars, mean) ``` ``` ## mpg cyl disp hp drat wt qsec ## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750 ## vs am gear carb ## 0.437500 0.406250 3.687500 2.812500 ``` ```r map_dbl(mtcars, mean) # controlla che l'output sia sempre numerico ``` ``` ## mpg cyl disp hp drat wt qsec ## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750 ## vs am gear carb ## 0.437500 0.406250 3.687500 2.812500 ``` ] --- # The amazing `tidyverse` Tutto questo (e molto altro) fa parte di un insieme di pacchetti per lavorare con i dati raccolti in un meta-pacchetto chiamato `tidyverse`. Questo permette di lavorare in molto molto più consistente ed intuitivo per manipolare, analizzare e rappresentare dati di tutti i tipi. <div class="figure" style="text-align: center"> <img src="data:image/png;base64,#img/tidyverse.svg" alt="<a href="https://www.tidyverse.org/">https://www.tidyverse.org/</a>" width="20%" /> <p class="caption"><a href="https://www.tidyverse.org/">https://www.tidyverse.org/</a></p> </div> --- # Come approfondire? Adesso abbiamo le competenze di base per capire qualsiasi nuova funzione/pacchetto in R. Per approfondire il `tidyverse` vi consiglio: <div class="figure" style="text-align: center"> <img src="data:image/png;base64,#img/r4ds.png" alt="<a href="https://r4ds.had.co.nz/">https://r4ds.had.co.nz/</a>" width="20%" /> <p class="caption"><a href="https://r4ds.had.co.nz/">https://r4ds.had.co.nz/</a></p> </div> --- # Come approfondire? Per capire a fondo tutte le cose che abbiamo visto ed espandere al massimo la conoscenza di R come linguaggio di programmazione vi consiglio il libro **advanced R** <div class="figure" style="text-align: center"> <img src="data:image/png;base64,#img/advancedR.jpg" alt="<a href="https://adv-r.hadley.nz/">https://adv-r.hadley.nz/</a>" width="20%" /> <p class="caption"><a href="https://adv-r.hadley.nz/">https://adv-r.hadley.nz/</a></p> </div> --- # Come approfondire? Anche `ggplot2` ha un suo libro associato che oltre a fornire un framework teorico sulle rappresentazioni grafiche in generale chiamato *grammar of graphics* spiega nel dettaglio il funzionamento del pacchetto <div class="figure" style="text-align: center"> <img src="data:image/png;base64,#img/ggplotbook.jpg" alt="<a href="https://ggplot2-book.org/">https://ggplot2-book.org/</a>" width="20%" /> <p class="caption"><a href="https://ggplot2-book.org/">https://ggplot2-book.org/</a></p> </div> --- class: section, center, middle # Altri argomenti utili... --- # Lavorare in modo avanzato con le stringhe Le stringhe sono uno strumento molto potente da conoscere per organizzare al meglio le vostre analisi. E' anche un argomento vasto e complesso ma questi riferimenti sono un buon punto di partenza: - [Capitolo 16](https://psicostat.github.io/Introduction2R/stringhe.html) del libro `introduction2R` - [Capitolo 14](https://r4ds.had.co.nz/strings.html) del libro `R4DS` - [Regex](https://cran.r-project.org/web/packages/stringr/vignettes/regular-expressions.html) ovvero Regular Expressions --- # Literate programming - R Markdown R Markdown è uno strumento estramente potente che permette di combinare testo e codice per creare documenti, slides (queste ad esempio), siti web, tesi, articoli scientifici etc. .pull-left[ <div class="figure" style="text-align: center"> <img src="data:image/png;base64,#img/knitr.svg" alt="<a href="https://cran.r-project.org/web/packages/knitr/index.html">knitr website</a>" width="50%" /> <p class="caption"><a href="https://cran.r-project.org/web/packages/knitr/index.html">knitr website</a></p> </div> ] .pull-right[ <div class="figure" style="text-align: center"> <img src="data:image/png;base64,#img/rmarkdown.svg" alt="<a href="https://rmarkdown.rstudio.com/">https://rmarkdown.rstudio.com/</a>" width="50%" /> <p class="caption"><a href="https://rmarkdown.rstudio.com/">https://rmarkdown.rstudio.com/</a></p> </div> ] --- # Creare tabelle Spesso dobbiamo creare delle tabelle da inserire nei documenti o in file R Markdown. Ci sono diversi pacchetti per questo ad esempio `flextable()`, `kableExtra()` o anche `sjPlot()`. Creiamo una tabella di statistiche descrittive: ```r df_sum <- iris |> group_by(Species) |> summarise(mean = mean(Sepal.Length), sd = sd(Sepal.Length), se = sd / sqrt(n()), min = min(Sepal.Length), max = max(Sepal.Length)) df_sum ``` ``` ## [90m# A tibble: 3 × 6[39m ## Species mean sd se min max ## [3m[90m<fct>[39m[23m [3m[90m<dbl>[39m[23m [3m[90m<dbl>[39m[23m [3m[90m<dbl>[39m[23m [3m[90m<dbl>[39m[23m [3m[90m<dbl>[39m[23m ## [90m1[39m setosa 5.01 0.352 0.049[4m8[24m 4.3 5.8 ## [90m2[39m versicolor 5.94 0.516 0.073[4m0[24m 4.9 7 ## [90m3[39m virginica 6.59 0.636 0.089[4m9[24m 4.9 7.9 ``` --- # Creare tabelle con `flextable` `flextable` permette di creare tabelle molto complesse e di salvarle in vari formati tra cui `docx` per poterle usare poi in formato `word`. Il punto di partenza è sempre un dataframe. Il sito di [`flextable`](https://ardata-fr.github.io/flextable-book/) contiene tantissa documentazione. ```r df_sum |> flextable() |> theme_vanilla() |> autofit() |> colformat_double(digits = 2) ``` <div class="tabwid"><style>.cl-8ca0656a{}.cl-8c9aba52{font-family:'DejaVu Sans';font-size:11pt;font-weight:bold;font-style:normal;text-decoration:none;color:rgba(0, 0, 0, 1.00);background-color:transparent;}.cl-8c9aba5c{font-family:'DejaVu Sans';font-size:11pt;font-weight:normal;font-style:normal;text-decoration:none;color:rgba(0, 0, 0, 1.00);background-color:transparent;}.cl-8c9cfd58{margin:0;text-align:left;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);padding-bottom:5pt;padding-top:5pt;padding-left:5pt;padding-right:5pt;line-height: 1;background-color:transparent;}.cl-8c9cfd62{margin:0;text-align:right;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);padding-bottom:5pt;padding-top:5pt;padding-left:5pt;padding-right:5pt;line-height: 1;background-color:transparent;}.cl-8c9d0ed8{width:1.031in;background-color:transparent;vertical-align: middle;border-bottom: 1.5pt solid rgba(102, 102, 102, 1.00);border-top: 1.5pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0ed9{width:0.76in;background-color:transparent;vertical-align: middle;border-bottom: 1.5pt solid rgba(102, 102, 102, 1.00);border-top: 1.5pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0ee2{width:1.111in;background-color:transparent;vertical-align: middle;border-bottom: 1.5pt solid rgba(102, 102, 102, 1.00);border-top: 1.5pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0ee3{width:1.209in;background-color:transparent;vertical-align: middle;border-bottom: 1.5pt solid rgba(102, 102, 102, 1.00);border-top: 1.5pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0eec{width:0.606in;background-color:transparent;vertical-align: middle;border-bottom: 1.5pt solid rgba(102, 102, 102, 1.00);border-top: 1.5pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0eed{width:0.646in;background-color:transparent;vertical-align: middle;border-bottom: 1.5pt solid rgba(102, 102, 102, 1.00);border-top: 1.5pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0ef6{width:1.031in;background-color:transparent;vertical-align: middle;border-bottom: 0.75pt solid rgba(102, 102, 102, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0ef7{width:0.76in;background-color:transparent;vertical-align: middle;border-bottom: 0.75pt solid rgba(102, 102, 102, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0f00{width:1.111in;background-color:transparent;vertical-align: middle;border-bottom: 0.75pt solid rgba(102, 102, 102, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0f01{width:1.209in;background-color:transparent;vertical-align: middle;border-bottom: 0.75pt solid rgba(102, 102, 102, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0f0a{width:0.606in;background-color:transparent;vertical-align: middle;border-bottom: 0.75pt solid rgba(102, 102, 102, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0f0b{width:0.646in;background-color:transparent;vertical-align: middle;border-bottom: 0.75pt solid rgba(102, 102, 102, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0f14{width:1.031in;background-color:transparent;vertical-align: middle;border-bottom: 0.75pt solid rgba(102, 102, 102, 1.00);border-top: 0.75pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0f15{width:0.76in;background-color:transparent;vertical-align: middle;border-bottom: 0.75pt solid rgba(102, 102, 102, 1.00);border-top: 0.75pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0f1e{width:1.111in;background-color:transparent;vertical-align: middle;border-bottom: 0.75pt solid rgba(102, 102, 102, 1.00);border-top: 0.75pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0f1f{width:1.209in;background-color:transparent;vertical-align: middle;border-bottom: 0.75pt solid rgba(102, 102, 102, 1.00);border-top: 0.75pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0f28{width:0.606in;background-color:transparent;vertical-align: middle;border-bottom: 0.75pt solid rgba(102, 102, 102, 1.00);border-top: 0.75pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0f32{width:0.646in;background-color:transparent;vertical-align: middle;border-bottom: 0.75pt solid rgba(102, 102, 102, 1.00);border-top: 0.75pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0f33{width:1.031in;background-color:transparent;vertical-align: middle;border-bottom: 1.5pt solid rgba(102, 102, 102, 1.00);border-top: 0.75pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0f3c{width:0.76in;background-color:transparent;vertical-align: middle;border-bottom: 1.5pt solid rgba(102, 102, 102, 1.00);border-top: 0.75pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0f3d{width:1.111in;background-color:transparent;vertical-align: middle;border-bottom: 1.5pt solid rgba(102, 102, 102, 1.00);border-top: 0.75pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0f46{width:1.209in;background-color:transparent;vertical-align: middle;border-bottom: 1.5pt solid rgba(102, 102, 102, 1.00);border-top: 0.75pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0f47{width:0.606in;background-color:transparent;vertical-align: middle;border-bottom: 1.5pt solid rgba(102, 102, 102, 1.00);border-top: 0.75pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-8c9d0f50{width:0.646in;background-color:transparent;vertical-align: middle;border-bottom: 1.5pt solid rgba(102, 102, 102, 1.00);border-top: 0.75pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}</style><table data-quarto-disable-processing='true' class='cl-8ca0656a'><thead><tr style="overflow-wrap:break-word;"><th class="cl-8c9d0ed8"><p class="cl-8c9cfd58"><span class="cl-8c9aba52">Species</span></p></th><th class="cl-8c9d0ed9"><p class="cl-8c9cfd62"><span class="cl-8c9aba52">mean</span></p></th><th class="cl-8c9d0ee2"><p class="cl-8c9cfd62"><span class="cl-8c9aba52">sd</span></p></th><th class="cl-8c9d0ee3"><p class="cl-8c9cfd62"><span class="cl-8c9aba52">se</span></p></th><th class="cl-8c9d0eec"><p class="cl-8c9cfd62"><span class="cl-8c9aba52">min</span></p></th><th class="cl-8c9d0eed"><p class="cl-8c9cfd62"><span class="cl-8c9aba52">max</span></p></th></tr></thead><tbody><tr style="overflow-wrap:break-word;"><td class="cl-8c9d0ef6"><p class="cl-8c9cfd58"><span class="cl-8c9aba5c">setosa</span></p></td><td class="cl-8c9d0ef7"><p class="cl-8c9cfd62"><span class="cl-8c9aba5c">5.01</span></p></td><td class="cl-8c9d0f00"><p class="cl-8c9cfd62"><span class="cl-8c9aba5c">0.35</span></p></td><td class="cl-8c9d0f01"><p class="cl-8c9cfd62"><span class="cl-8c9aba5c">0.05</span></p></td><td class="cl-8c9d0f0a"><p class="cl-8c9cfd62"><span class="cl-8c9aba5c">4.30</span></p></td><td class="cl-8c9d0f0b"><p class="cl-8c9cfd62"><span class="cl-8c9aba5c">5.80</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-8c9d0f14"><p class="cl-8c9cfd58"><span class="cl-8c9aba5c">versicolor</span></p></td><td class="cl-8c9d0f15"><p class="cl-8c9cfd62"><span class="cl-8c9aba5c">5.94</span></p></td><td class="cl-8c9d0f1e"><p class="cl-8c9cfd62"><span class="cl-8c9aba5c">0.52</span></p></td><td class="cl-8c9d0f1f"><p class="cl-8c9cfd62"><span class="cl-8c9aba5c">0.07</span></p></td><td class="cl-8c9d0f28"><p class="cl-8c9cfd62"><span class="cl-8c9aba5c">4.90</span></p></td><td class="cl-8c9d0f32"><p class="cl-8c9cfd62"><span class="cl-8c9aba5c">7.00</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-8c9d0f33"><p class="cl-8c9cfd58"><span class="cl-8c9aba5c">virginica</span></p></td><td class="cl-8c9d0f3c"><p class="cl-8c9cfd62"><span class="cl-8c9aba5c">6.59</span></p></td><td class="cl-8c9d0f3d"><p class="cl-8c9cfd62"><span class="cl-8c9aba5c">0.64</span></p></td><td class="cl-8c9d0f46"><p class="cl-8c9cfd62"><span class="cl-8c9aba5c">0.09</span></p></td><td class="cl-8c9d0f47"><p class="cl-8c9cfd62"><span class="cl-8c9aba5c">4.90</span></p></td><td class="cl-8c9d0f50"><p class="cl-8c9cfd62"><span class="cl-8c9aba5c">7.90</span></p></td></tr></tbody></table></div> --- # Creare tabelle con `kableExtra` `kableExtra` è un altro ottimo pacchetto molto utilizzato per documenti R Markdown per creare tabelle in **html** e **pdf**. La documentazione per tabelle in [**html**](https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html) e [**pdf**](https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_pdf.pdf) è ottima. Si parte sempre da un dataframe: ```r df_sum |> kable() |> kable_styling(bootstrap_options = c("striped"), full_width = FALSE) ``` <table class="table table-striped" style="color: black; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> Species </th> <th style="text-align:right;"> mean </th> <th style="text-align:right;"> sd </th> <th style="text-align:right;"> se </th> <th style="text-align:right;"> min </th> <th style="text-align:right;"> max </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> setosa </td> <td style="text-align:right;"> 5.006 </td> <td style="text-align:right;"> 0.3524897 </td> <td style="text-align:right;"> 0.0498496 </td> <td style="text-align:right;"> 4.3 </td> <td style="text-align:right;"> 5.8 </td> </tr> <tr> <td style="text-align:left;"> versicolor </td> <td style="text-align:right;"> 5.936 </td> <td style="text-align:right;"> 0.5161711 </td> <td style="text-align:right;"> 0.0729976 </td> <td style="text-align:right;"> 4.9 </td> <td style="text-align:right;"> 7.0 </td> </tr> <tr> <td style="text-align:left;"> virginica </td> <td style="text-align:right;"> 6.588 </td> <td style="text-align:right;"> 0.6358796 </td> <td style="text-align:right;"> 0.0899270 </td> <td style="text-align:right;"> 4.9 </td> <td style="text-align:right;"> 7.9 </td> </tr> </tbody> </table> --- # Creare tabelle da modelli statistici con `sjPlot` [`sjPlot`](https://cran.r-project.org/web/packages/sjPlot/index.html) è un pacchetto tuttofare che tra le altre cose crea grafici e tabelle partendo non da dataframe ma da oggetti di modelli inserendo una serie di informazioni utili in automatico. ```r fit <- lm(Sepal.Length ~ Petal.Length, data = iris) sjPlot::tab_model(fit) ``` <table style="border-collapse:collapse; border:none;"> <tr> <th style="border-top: double; text-align:center; font-style:normal; font-weight:bold; padding:0.2cm; text-align:left; "> </th> <th colspan="3" style="border-top: double; text-align:center; font-style:normal; font-weight:bold; padding:0.2cm; ">Sepal.Length</th> </tr> <tr> <td style=" text-align:center; border-bottom:1px solid; font-style:italic; font-weight:normal; text-align:left; ">Predictors</td> <td style=" text-align:center; border-bottom:1px solid; font-style:italic; font-weight:normal; ">Estimates</td> <td style=" text-align:center; border-bottom:1px solid; font-style:italic; font-weight:normal; ">CI</td> <td style=" text-align:center; border-bottom:1px solid; font-style:italic; font-weight:normal; ">p</td> </tr> <tr> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">(Intercept)</td> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">4.31</td> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">4.15 – 4.46</td> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; "><strong><0.001</strong></td> </tr> <tr> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">Petal Length</td> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">0.41</td> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">0.37 – 0.45</td> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; "><strong><0.001</strong></td> </tr> <tr> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; padding-top:0.1cm; padding-bottom:0.1cm; border-top:1px solid;">Observations</td> <td style=" padding:0.2cm; text-align:left; vertical-align:top; padding-top:0.1cm; padding-bottom:0.1cm; text-align:left; border-top:1px solid;" colspan="3">150</td> </tr> <tr> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; padding-top:0.1cm; padding-bottom:0.1cm;">R<sup>2</sup> / R<sup>2</sup> adjusted</td> <td style=" padding:0.2cm; text-align:left; vertical-align:top; padding-top:0.1cm; padding-bottom:0.1cm; text-align:left;" colspan="3">0.760 / 0.758</td> </tr> </table> --- # Creare tabelle da modelli statistici con `broom` [`broom`](https://cran.r-project.org/web/packages/broom/vignettes/broom.html) ha un approccio diverso (quello che preferisco) creando dei dataframe da modelli statistici e poi lasciando all'utente la creazione della tabella con `flextable` o `kableExtra`: .pull-left[ <div class="figure" style="text-align: center"> <img src="data:image/png;base64,#img/broom.svg" alt="<a href="https://broom.tidymodels.org">https://broom.tidymodels.org</a>" width="50%" /> <p class="caption"><a href="https://broom.tidymodels.org">https://broom.tidymodels.org</a></p> </div> ] .pull-right[ ```r tidfit <- broom::tidy(fit) tidfit ``` ``` ## [90m# A tibble: 2 × 5[39m ## term estimate std.error statistic p.value ## [3m[90m<chr>[39m[23m [3m[90m<dbl>[39m[23m [3m[90m<dbl>[39m[23m [3m[90m<dbl>[39m[23m [3m[90m<dbl>[39m[23m ## [90m1[39m (Intercept) 4.31 0.078[4m4[24m 54.9 2.43[90me[39m[31m-100[39m ## [90m2[39m Petal.Length 0.409 0.018[4m9[24m 21.6 1.04[90me[39m[31m- 47[39m ``` ] --- # Materiale Nel sito del corso [arca-dpss.github.io/course-R](https://arca-dpss.github.io/course-R/) è presente una sezione con articoli, libri e materiale di approfondimento di tutto quello che abbiamo visto e di questi argomenti extra. Nel tempo questo materiale potrebbe crescere ma sarà sempre disponibile nel link indicato 😄