vuoi
o PayPal
tutte le volte che vuoi
FUNZIONI PER LA MANIPOLAZIONE DEI VETTORI
> x <- c(2, 5, 9.5, -3)>
> x[1] 2.0 5.0 9.5 -3.0>
> length(x)[1] 4>
> min(x)[1] -3>
> max(x)[1] 9.5>
> sum(x)[1] 13.5>
> mean(x)[1] 3.375>
MATRICI
> X <- matrix(c(+ 2, 4,+ 1, 2,+ 6, 5,+ 7, 1), ncol=2, byrow=T)>
> X [,1] [,2][1,] 2 4[2,] 1 2[3,] 6 5[4,] 7 1>
> dim(X)[1] 4 2>>
> X[3, ][1] 6 5>
> X[ ,2][1] 4 2 5 1>
> X[1,2][1] 4
FATTORI (Vettori con elementi alfanumerici)
> F1 <- factor( c('a', 'a', 'b', 'c', 'a') )>
> F1[1] a a b c aLevels: a b c>
> x <- c(2, 5, 9.5, -3)>
> x[1] 2.0 5.0 9.5 -3.0>
> is.factor(x)[1] FALSE>
> is.factor(F1)[1] TRUE>
DATA-FRAME (Risultati sia alfanumerici che qualitativi)
> cognome <- factor( c('Rossi', 'Bianchi', 'Verdi', 'Neri', 'Arancio') )>
> cognome[1] Rossi Bianchi Verdi Neri ArancioLevels: Arancio Bianchi Neri Rossi Verdi>
> voto <- c(29, 30, 24, 26, 18)>
<voto[1]> 29 30 24 26 18>
<genere <- factor( c('m','f','f','m','f') )>
<genere[1]> m f f m fLevels: f m>
<dati <- data.frame(cognome, genere, voto)>
<daticognome genere voto1 Rossi m 292 Bianchi f 303 Verdi f 244 Neri m 265 Arancio f 18>
<dati$voto[1]> 29 30 24 26 18>
<mean(dati$voto)[1]> 25.4
<FUNZIONE class() (Tipo di classe di oggetti a cui appartiene l’oggetto)>
<a <- 2>
<a[1]> 2>
<class(a)[1]> "numeric"
<x <- c(2, 5, 9.5, -3)>
<x[1]> 2.0 5.0 9.5 -3.0>
<class(x)[1]> "numeric"
<X <- matrix(c(+ 2, 4,+ 1, 2,+ 6, 5,+ 7, 1), ncol=2, byrow=T)>
<X [,1] [,2][1,] 2 4[2,] 1 2[3,] 6 5[4,] 7 1>
<class(X)[1]> "matrix"
<F1 <- factor( c('a', 'a', 'b', 'c', 'a') )>
<F1[1]> a a b c aLevels: a b c>
<class(F1)[1]> "factor"
<cognome <- factor( c('Rossi', 'Bianchi', 'Verdi'
<code> 'Neri', 'Arancio') )> cognome[1] Rossi Bianchi Verdi Neri ArancioLevels: Arancio Bianchi Neri Rossi Verdi> voto <- c(29, 30, 24, 26, 18)> voto[1] 29 30 24 26 18> genere <- factor( c('m','f','f','m','f') )> genere[1] m f f m fLevels: f m> dati <- data.frame(cognome, genere, voto)> daticognome genere voto1 Rossi m 292 Bianchi f 303 Verdi f 244 Neri m 265 Arancio f 18> class(dati)[1] "data.frame">MEDIA> y <- c(2, 3, 5, 1, 9)> y[1] 2 3 5 1 9> mean(y)[1] 4> (2+3+5+1+9)/5[1] 4> sum(y)/5[1] 4> sum(y)/length(y)[1] 4VARIANZA> y <- c(2, 3, 5, 1, 9)> y[1] 2 3 5 1 9> var(y)[1] 10>> mean(y)[1] 4> scarti <- y - mean(y)> scarti[1] -2 -1 1 -3 5> scarti^2[1] 4 1 1 9 25> sum(scarti^2)[1] 40>> sum(scarti^2) / (5-1)[1] 10>DEVIAZIONE STANDARD> y <- c(2, 3, 5, 1, 9)> y[1] 2 3 5 1 9> mean(y)[1] 4> scarti <- y - </code>
mean(y) > scarti[1] -2 -1 1 -3 5 > scarti^2[1] 4 1 1 9 25 > sum(scarti^2)[1] 40 >> sd(y)[1] 3.162278 >> sqrt( sum( (y-mean(y))^2)/(length(y)-1))[1] 3.162278 >FUNZIONE summary() (Con un’unica istruzione dà 5 fondamentali statistiche descrittive) > y <- c(2, 3, 5, 1, 9) > y[1] 2 3 5 1 9 >> summary(y) Min. 1st Qu. Median Mean 3rd Qu. Max. 1 2 3 4 5 9 >TEST t DI STUDENT > y <- c(2, 3, 5, 1, 9) > y[1] 2 3 5 1 9 > t.test(y, mu=3, var.equal = TRUE) One Sample t-test data: y t = 0.7071, df = 4, p-value = 0.5185 alternative hypothesis: true mean is not equal to 3 95 percent confidence interval: 0.07351368 7.92648632 sample estimates: mean of x 4 OPERATORE : (Genera sequenze di numeri interi) > 1:4 [1] 1 2 3 4 > y <- c(2, 3, 5, 1, 9) > y[1] 2 3 5 1 9 > y[2] [1] 3 > y[2:4] [1] 3 5 1 >FUNZION seq() (Genera sequenze di numeri più complesse) > seq(2, 8, by=2) [1] 2 4 6 8 > seq(0, 1, by=.1) [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
- 1.0> seq(1, 30, length.out=10)[1] 1.000000 4.222222 7.444444 10.666667 13.888889 17.111111 20.333333 23.55555626.777778 30.000000>
- FUNZIONE rep()> rep(c(1, 2, 3), each=3)[1] 1 1 1 2 2 2 3 3 3>
- f1 <- factor( rep(c('a', 'b', 'c'), each=3) )> f1[1] a a a b b b c c cLevels: a b c>
- IMPORTAZIONE DATI PACCHETTI> library(car)>
- data(Prestige)>
- dim(Prestige)[1] 102 6>
- Prestige[1:5, ]education income women prestige census typegov.administrators 13.11 12351 11.16 68.8 1113 profgeneral.managers 12.26 25879 4.02 69.1 1130 profaccountants 12.77 9271 15.70 63.4 1171 profpurchasing.officers 11.42 8865 9.11 56.8 1175 profchemists 14.62 8403 11.68 73.5 2111 prof>
- summary(Prestige)education income women prestige census typeMin. : 6.380 Min. : 611 Min. : 0.000 Min. :14.80 Min. :1113 bc :441st Qu.: 8.445 1st Qu.: 4106 1st Qu.: 3.592 1st Qu.:35.23 1st Qu.:3120 prof:31Median :10.540 Median : 5930 Median :13.600 Median :43.60 Median :5135 wc
Mean :23.738 Mean :6798 Mean :28.979 Mean :46.83 Mean :5402 NA's: 43rd Qu.:12.648 3rd Qu.: 8187 3rd Qu.:52.203 3rd Qu.:59.27 3rd Qu.:8312 Max. :15.970 Max. :25879 Max. :97.510 Max. :87.20 Max. :9517 <ISTOGRAMMA> cognome <- factor( c('Rossi', 'Bianchi', 'Verdi', 'Neri', 'Arancio') ) cognome[1] Rossi Bianchi Verdi Neri Arancio Levels: Arancio Bianchi Neri Rossi Verdi voto <- c(29, 30, 24, 26, 18) voto[1] 29 30 24 26 18 genere <- factor( c('m','f','f','m','f') ) genere[1] m f f m f Levels: f m dati <- data.frame(cognome, genere, voto) daticognome genere voto 1 Rossi m 29 2 Bianchi f 30 3 Verdi f 24 4 Neri m 26 5 Arancio f 18 dati$voto