






Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!







local({fn<-choose.files(filters=Filters[c('R','txt','All'),],index=4)
4^2-3* [1] 10 (4^2)-(3*2) [1] 10 2^- [1] 0. -2-- [1] 1 -2 - - [1] 1 sqrt(9) [1] 3
SIMBOLO DI ASSEGNAZIONE
a <- 2 a [1] 2
MAIUSCOLE E MINUSCOLE (differenza)
a <- 2 A Errore: oggetto "A" non trovato a [1] 2
x <- c(2, 5, 9.5,-3) x [1] 2.0 5.0 9.5 -3.
OPERAZIONI ARITMETICHE SUI VETTORI (elemento per elemento)
x <- c(2, 5, 9.5, -3) x [1] 2.0 5.0 9.5 -3. x+ [1] 4.0 7.0 11.5 -1. x/ [1] 1.00 2.50 4.75 -1.
a <- 2 a [1] 2 a*x [1] 4 10 19 -
SOMMA TRA 2 VETTORI (stesso numero di elementi)
x <- c(2, 5, 9.5, -3) x [1] 2.0 5.0 9.5 -3. y <- c(2, 1, -2, 3) y [1] 2 1 -2 3
x+y [1] 4.0 6.0 7.5 0.
ESTRAZIONE DI ELEMENTI DI UN VETTORE (specificando la posizione degli elementi estratti, tra parentesi quadre)
x <- c(2, 5, 9.5, -3) x [1] 2.0 5.0 9.5 -3.
x[2] [1] 5
x[2]/x[1] [1] 2. x[1:3] [1] 2.0 5.0 9.
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 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) dati cognome genere voto 1 Rossi m 29 2 Bianchi f 30
3 Verdi f 24 4 Neri m 26 5 Arancio f 18
dati$voto
[1] 29 30 24 26 18
mean(dati$voto) [1] 25. 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. class(x) [1] "numeric"
X <- matrix(c(
X
class(X) [1] "matrix"
F1 <- factor( c('a', 'a', 'b', 'c', 'a') ) F [1] a a b c a Levels: a b c class(F1) [1] "factor"
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) dati cognome genere voto 1 Rossi m 29 2 Bianchi f 30 3 Verdi f 24 4 Neri m 26 5 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)/ [1] 4 sum(y)/ [1] 4 sum(y)/length(y) [1] 4
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. alternative hypothesis: true mean is not equal to 3 95 percent confidence interval: 0.07351368 7. sample estimates: mean of x 4 OPERATORE : (Genera sequenze di numeri interi) 1: [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. seq(1, 30, length.out=10) [1] 1.000000 4.222222 7.444444 10.666667 13.888889 17.111111 20.333333 23. 26.777778 30.
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) )
f [1] a a a b b b c c c Levels: a b c
IMPORTAZIONE DATI PACCHETTI
library(car) data(Prestige) dim(Prestige) [1] 102 6 Prestige[1:5, ] education income women prestige census type gov.administrators 13.11 12351 11.16 68.8 1113 prof general.managers 12.26 25879 4.02 69.1 1130 prof accountants 12.77 9271 15.70 63.4 1171 prof purchasing.officers 11.42 8865 9.11 56.8 1175 prof chemists 14.62 8403 11.68 73.5 2111 prof summary(Prestige) education income women prestige census type Min. : 6.380 Min. : 611 Min. : 0.000 Min. :14.80 Min. :1113 bc : 1st Qu.: 8.445 1st Qu.: 4106 1st Qu.: 3.592 1st Qu.:35.23 1st Qu.:3120 prof: Median :10.540 Median : 5930 Median :13.600 Median :43.60 Median :5135 wc : 23 Mean :10.738 Mean : 6798 Mean :28.979 Mean :46.83 Mean :5402 NA's: 4 3rd Qu.:12.648 3rd Qu.: 8187 3rd Qu.:52.203 3rd Qu.:59.27 3rd Qu.: Max. :15.970 Max. :25879 Max. :97.510 Max. :87.20 Max. :
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) dati cognome genere voto 1 Rossi m 29 2 Bianchi f 30 3 Verdi f 24
4 Neri m 26 5 Arancio f 18
dati$voto
FUNZIONE rnorm (n,mu,sigma) (Per generare un campione di dimensione n da una normale con media mu e deviazione standard sigma)
rnorm(10, 24, 3) [1] 21.28975 20.42996 25.83862 19.66184 25.38103 23.33875 30.79766 27. 23.80436 24.
rnorm(10, 24, 3) [1] 25.31715 25.63835 21.55983 29.51071 19.16178 28.78043 23.71342 19. 21.11454 19.