###################################################################### # Initial examples of how to use R # # This comes from my book: Analysis of Categorical Data with R # ###################################################################### ###################################################################### #Section 1 2+2 pnorm(1.96) (2-3)/6 2^2 sin(pi/2) log(1) save<-2+2 save ls() objects() ###################################################################### #Section 2 x <- c(1,2,3,4,5) sd2 <- function(numbers) { sqrt(var(numbers)) } sd2(x) sd2 <- function(numbers) { cat("Print the data \n", numbers, "\n") sqrt(var(numbers)) } save <- sd2(x) save #Another exmaple of calculating the standrd deviation where all of the function's code is on one line. # The semicolon is used to separate cat() and sqrt() function calls. This symbol is used # to signal the end of a complete line of code (rarely is there a need for it). sd3 <- function(numbers) { cat("Print the data \n", numbers, "\n"); sqrt(var(numbers)) } sd3(x) ###################################################################### #Section 3 pnorm(1.96) pnorm(q = 1.96) pnorm(1.96, 0, 1) pnorm(q = 1.96, mean = 0, sd = 1) ###################################################################### #Section 4 pnorm(q = c(-1.96, 1.96)) x <- c(3.68, -3.63, 0.80, 3.03, -9.86, -8.66, -2.38, 8.94, 0.52, 1.25) y <- c(0.55, 1.65, 0.98, -0.07, -0.01, -0.31, -0.34, -1.38, -1.32, 0.53) x y x + y # Elementwise addition x * y # Elementwise multiplication mean(x) x - mean(x) # Each element of x has the mean of x subtracted x * 2 # Each element of x is multiplied by 2 # Other languages may require the following to add x and y x[1] # First element of x y[1] # First element of y x[1] + y[1] x[2] + y[2] x[8] + y[8] #