




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!





Import data set: se txt: data <-read.delim(“data.txt”,header=TRUE,sep=””,row.names”text”) se csv: data <- read.csv(“data.csv”,header=TRUE) se tsv : data <- read_tsv(data.tsv) Create a tibble : library(tidyverse) library(tibble) str(data) as_tibble(data)< Use as_tibble(): external file and you want to work with that with tidyverse, you may transform it as tibble data.t <- as_tibble(data) str(data.t) you can explicitly print() the data frame and control the number of rows (n) nycflights13::flights %>% print(n = 15, width = Inf) Subsetting
ggplot(data = mpg) + geom_smooth(mapping = aes(x = displ, y = hwy, linetype = drv), show.legend = FALSE) To display multiple geoms in the same plot, add multiple geom functions to ggplot(): ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + geom_smooth(mapping = aes(x = displ, y = hwy)) #You can color a bar chart using either the colour aesthetic, or fill: #The colour option colors the edge ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, colour = cut)) #line chart: geom_line() #boxplot: geom_boxplot() #histogram: geom_histogram() #area chart: geom_area() Data transformation filter() #Pick observations by their values arrange() #Reorder the rows select() #Pick variables by their names mutate() #Create new variables with functions of existing variables summarise() #Collapse many values down to a single summary group_by() #changes the scope of each function from operating on the entire dataset to t group-by-group filter filter(flights, month == 1, day == 1) It selects all flights on January 1st jan1 <- filter(flights, month == 1, day == 1) print(jan1) filter(flights, month == 11 | month == 12) #Finds all flights that departed in November or Decembe #If you want to preserve missing values, ask for them explicitly
#Foreign key
es. b <- 0 for (i in 1:10){ b <- b + i} b #while loop while (test_expression){ statement} Examples: a <-0 while (a >0) { print(a) a <- a - 1} a #next if (test_condition) { next} #Examples: x <- 1:5 for (val in x) { if (val == 3){next } print(val)} #break if (test_expression) { break} exemples: a <- 5 for (i in 1:10){ if (i == 2) break a <- a - i } a #Repeat loop repeat {statement} Example: x <- 1 repeat {print(x) x = x+1 if (x == 6){ break }} #Functions Functions are used to logically break our code into simpler parts which become easy to maintain and understand. Exemple : my_func <- function(){ print('hello') } my_func Graphs review # We can put the two graphs in the same page using split split = c(x, y, nx, ny) #Plots barplot(table(X)) #Bar plot plot(table(Z)) #Stick plot pie(table(Y)) #Pie plot bubbleplot(table(X,Y)) #Bubbleplot plot(Z,W) #Scatter plot text(8,40,expression(y[i]==8.87 - 0.093*x[i])) #Boxplot boxplot( Sepal.Length ~ Species, main = "Iris", las = 1, layout=c(1,6),) #Q-Q plot qqnorm(x,col = 2) qqline(x,col = 4,lwd = 2) qqplot(x,rt(200,df=1)) #Histograms hist(Sepal.Length, main = "", density = FALSE, freq = TRUE) < con frequenze assolute) hist( W, breaks=3 ) A density line lines(density(Sepal.Length), col = "blue", lwd = 2) #Barplot barplot(VADeaths, beside = TRUE, las = 1, col = c("lightblue", "mistyrose", "lightcyan", ylim= c(0,120)) #Pairs Pairs is used to visualize the scatter plot of multiple variables taken by pairs pairs(iris[1:4], main =””,cex = 1.5, cex.labels = 2, font.labels = 2, pch = 21, ) sequence seq(-3,6,2, len=11) #sequence of values from -3 to 6, pace 2, length= which((x >= -1) & (x < 5)) # & means AND which((x < -2) | (x > 1)) # | means OR x[index] #extract the values cmp <- complex(real=1:10,imaginary=-1:9) cmp # defines a complex number
cumsum(table(X)) #cumulative frequencie #Continuous variables table(cut(W, breaks=c(40,50,58,70,95))) # data organized in classes with suitable breakpoints classes <- c(40,50,58,70,95) hist(W, br=classes, plot = FALSE) #it provides additional information, included the density of frequency table(X,Y) #double distribution table(X, cut(W,br=classes)) table(X,Y,Z) #three-way contingency table ftable(X,Y,Z) #creates a flat contingency table margin.table(tab,1) #marginal distribution of X prop.table(tab) #joint relative frequency distribution prop.table(tab,1) #conditional distribution of Y|X (relative) #Kernel density estimation plot(density(W)) #Kernel density estimation (optimal bandwidth) plot(density(W,bw=3)) #Kernel density estimation (fixed bandwidth) density(W) #Descriptive statistics summary(X) #Frequency distribution for a qualitative variable summary(Z) #Quartiles,Range,Mean summary(table(X,Y)) #Chi-square test of independence median(W) #Median quantile(W, c(0.1,0.3,0.7,0.93,.98)) #Quantiles var(W) #Sample variance of W cor(Z,W,method='pearson') #Correlation coefficient, method=Pearson cor(Z,W,method='spearman') #Correlation coefficient, method=Spearman Operazioni sqrt(16)< radice quadrata log(100,base=10)< logaritmo base 10 exp(2)< esponenziale cos(0)< coseno sin(pi/2) tan(pi/4) abs(-3) < valore assoluto factorial(5)< fattoriale choose(10,4)< scegli tra 10 a 4 un n curve(5-3x^2+6x, add=TRUE,lty=2)< crea una curva par(mfrow=c(2,2)) < parabola con max tabella 2x curve(sqrt,0,100) title("Square root")< curva con radice curve(cos, -2pi, 2pi,add=TRUE, lty=2)< curva con pi greco In a ballot there are 7 white balls and 3 black balls. We draw “in block” (i.e., without replacement) 4 balls. The experiment consists of evaluating how many white balls have been drawn. Use a for loop to repeat this experiment 1,000 times. Evaluate your results.