# Q1.1 We can use as a Calculator 3 + 4*9 4*log(sqrt(5))/sin(2.5*pi) - exp(-3^2) # Q1.2 We can use R to construct functions and manipulate them dat <- c(5, 1, 2, 13, 5, 8, 3, 1, 34, 21) # We create this data using the combine command c(). dat sum(dat) # sum of data length(dat) # length of data mean(dat) # mean of data sd(dat) # SD of data dat - mean(dat) # Transform data: De-mean data # Q1.3 We define new variables, by manipulating the data m_dat <- mean(dat) sd_dat <- sd(dat) z_dat <- (dat - m_dat)/sd_dat # We standardized dat z_dat dat_m_2 <- (dat - mean(dat))^2 # We square the deviations from mean n_dat <-length(dat) var_dat <- sum(dat_m_2)/(n_dat-1) var_dat # Q1.4 We can write functions in R var_f <- function(dat_f) { m_dat_f <-mean(dat_f) return(sum((dat_f-m_dat_f)^2)/(length(dat_f)- 1)) } var_f(dat) sqrt(var_f) # Q1.5 Quick Summary of Distribution and Sorting summary(dat) sort(dat) # Q1.6 In general, we will read the data from a data file. But, we can create in different ways: dat_1 <- rep(6,10) dat_2 <- seq(from=1, to=25, by=5) dat_3 <- runif(10) # Generate 10 random values from a Uniform(0,1) dat_4 <- rnorm(10, mean=2, sd=2) # Generate 10 random values from a Normal(2,4) dat_5 <- c(dat_1, dat_2, dat_3, dat_4) # Combine all the data together # Q1.7 We create a matrix, using rbind() ("row bind") or cbind() ("column bind"): A <- rbind(dat_1,dat_3, dat_4) # 3 rows, 10 columns A B <- cbind(dat_1,dat_4, dat_4) # 10 rows, 3 columns B # Q1.8 Graphs plot(dat, type="l", main="Plot of Data") # All the data consecutively, only interesting if a time series dat_prop <- dat/n_dat # Data as a proportion of size hist_d <- hist(dat_prop, breaks=6, main="Histogram with breaks", xlab="Generated Data") lines(density(dat_prop), col='red') # lines makes a curve, default bandwidth