### Numbers and their operations x <- 5 class(x) y <- 6 x+y x-y x*y x/y ### Vectors and their operations v <- c(1,2,3,4) class(v) length(v) u <- c(-2,1,3,2) u+v u-v u*v u/v # dot product u%*%v # or t(u)%*%v # multiplying vectors to obtain a matrix u%*%t(v) ### Matrices and their operations A <- matrix(NA,2,3) for (i in 1:2){ for(j in 1:3){A[i,j]=2 } } A # Alternatively, we could also do A <- matrix(2,2,3) A # This is slightly different from what we did in the tutorial, but see if you can understand B <- matrix(NA,3,2) for (i in 1:3){ for(j in 1:2){B[i,j]=i+j } } B # Extract a row or a column from a matrix B[1,] B[,2] # Multiply a matrix onto a vector w <- c(1,2,3) t(B)%*%w # Turn a matrix into a data frame B_df <- as.data.frame(B) B_df # Extract a vector of variables from a data frame B_df$V2 ### Median, Mean, Variance # Recall the intuition why we need to consider mean together with variance a <- c(1,2,4,3,7,5,9,11,15,17,19,13,13,12,16,9,14,13,16,2) # Order a in ascending order sort(a) median(a) mean(a) var(a) ### Boxplot boxplot(a) ### Histogram hist(a) # Generate randome numbers from a distribution # you are not required to know the beta distribution, focus on understanding the histogram b <- rbeta(10000,2,5) hist(b) boxplot(b)