Introduction to R - Introduzione a R
Introduction to R - Introduzione a R
Getting Help
Several resources are available to help you learn more about R. These include several facilities WITHIN R itself and, of course, on the Web. The help() allows to find information of a given function for example mean by typing
> help(mean)
or
> ?mean
For special characters and some reserved words one has to use quote For example, if you have problems with < operator type
> help("<")
For example, if the problem is with the reserved word if (basic control-flow constructs see later), type
> ?"if"
The example() function will isolate the examples in the help page for the given function.
> example(mean)
mean> x <- c(0:10, 50)
mean> xm <- mean(x)
mean> c(xm, mean(x, trim = 0.10))
[1] 8.75 5.50
> example("if")
if> for(i in 1:5) print(1:i)
[1] 1
[1] 1 2
[1] 1 2 3
[1] 1 2 3 4
[1] 1 2 3 4 5
if> for(n in c(2,5,10,20,50)) {
if+ x <- stats::rnorm
if+ cat(n, ": ", sum(x^2), "\n", sep = "")
if+ }
2: 0.2666525
5: 3.512141
10: 8.711755
20: 12.94274
50: 52.22404
if> f <- factor(sample(letters[1:5], 10, replace = TRUE))
if> for(i in unique(f)) print(i)
[1] "e"
[1] "b"
[1] "a"
[1] "c"
[1] "d"
The help.search() function finds information when the help() and example() functions fail or one doesn't know what exactly what to look for. It is a Google- style search through R documentation. For instance, say you need a function to draw three-dimensional plots. Type help.search('3d plots') R finds the packages that could contain functions for drawing 3d plots ... however it takes just forever to find them locally! Use the web instead.
R is organized in packages or library as illustrated in more details in section 2.3. To learn about entire packages it is enough to type help(package=PACKAGE's NAME). For example, for the package "graphics"
> help(package=graphics)
while to get the list of all commands and datasets in a package just type library(help=name of the package)
> library(help=graphics)
Notice that all the commands introduced in this text are available as session script and there more examples of help search are reported.
Ottenere aiuto
Diverse risorse sono disponibili per conoscere meglio l'ambiente di R. Queste comprendono risorse INCLUSE in R e, ovviamente, sul Web. La funzione help() consente di reperire informazioni su un'altra funzione, come ad esempio la funzione mean(media).
> help(mean)
oppure
> ?mean
Per quanto riguarda caratteri speciali o parole riservate é necessario usare le virgolette ("). Per esempio se si vuole aiuto per l'operatore < si digita
> help("<")
Per esempio, se si ha un problema con la parola riservata if (controllo di flusso base), digitare
> ?"if"
La funzione example() estrae gli esempi dalla pagina di help per una data funzione:
> example(mean)
mean> x <- c(0:10, 50)
mean> xm <- mean(x)
mean> c(xm, mean(x, trim = 0.10))
[1] 8.75 5.50
> example("if")
if> for(i in 1:5) print(1:i)
[1] 1
[1] 1 2
[1] 1 2 3
[1] 1 2 3 4
[1] 1 2 3 4 5
if> for(n in c(2,5,10,20,50)) {
if+ x <- stats::rnorm
if+ cat(n, ": ", sum(x^2), "\n", sep = "")
if+ }
2: 0.2666525
5: 3.512141
10: 8.711755
20: 12.94274
50: 52.22404
if> f <- factor(sample(letters[1:5], 10, replace = TRUE))
if> for(i in unique(f)) print(i)
[1] "e"
[1] "b"
[1] "a"
[1] "c"
[1] "d"
La funzione help.search() trova informazioni quando le funzioni help() e example() non riescono a reperire informazioni adeguate o si ha la necessitá di fare una ricerca piú libere. Si tratta di una ricerca, stile Google, effettuata in tutta la documentazion di R. Ad esempio, si ha bisogno di una funzione che disegna grafici (plot) 3D. Si digita help.search('3d plots') e R trova tutti i pacchetti che consento il disegno di grafici 3D ... comunque, alcune volte la ricerca in locale é molto lunga! Usare per le ricerche il web.
R é organizzato in pacchetti o librerie, come si vedrá in dettaglio nella sezione successiva. Per conoscere tutto di un pachetto é sufficiente digitare help(package=NOME PACCHETTO). Per esempio, per il pacchetto "grphics"
> help(package=graphics)
mentre per avere un elenco di tutti i comandi e i dataset presenti in un pacchetto é sufficiente digitare library(help=NOME PACCHETTO)
> library(help=graphics)
Si tenga presente che tutti i comandi usati in questa sezione sono disponibili come script di sessione, dove sono presenti ulteriori esempi sulle ricreche di help.