R is a programming language that is widely used in areas of scientific activity and quantitative research.
- It is open source
- It works on all platforms
- It has extensive functionality
The extensive functionally is through R packages, which are referred to as Libraries when installed. These libraries extend the core R packages.
The Basics
You can type commands directly in the R console but you are strongly encouraged to always work with a script.
- Create a new script:
- File -> New
- Save right away and often (this just good practice)
Basic Arithmetic
Type the following in your script
2 + 3 6 - 3 9 / 3 11 * 3 12^3
- Try the following:
- Press Control+Enter when the cursor on one of the lines
- Select a few lines and then press Control+Enter
- Select all the lines and then press Control+Enter
- Repeat the steps above using the Run button at the top of the script window
- Try the following:
Variable Assignment
It is convenient and helpful to assign values to variables. The form is: variable <- value
x <- 3 y <- 6 x - y z <- y + x sqrt(z)
Try: Before doing anything else, put your cursor at the of the the third line and press Control + Enter. You will probably get an error, it is important to note that the code is executed sequentially.
The basic assignment type in R is to a vector of values. A vector can be a single value shown with x, y, z above or multiple values.
heights <- c(12.2, 15.6, 16.2, 18.3, 11.5, 14.3) heights heights**2 sum(heights) mean(heights)
c instructs R to concatenate multiple values.
Try to find the squareroots of the heights; the first element; elements 3, 2, and 4; elements 2 to 5, the maximum value.
- An exercise to try:
- Create a vector bccities of the following cities in BC: Prince George, Quesnel, Chilliwack, Vancouver, Kelowna
- Create a vector bcinterior with logical values: TRUE, TRUE, FALSE, FALSE, TRUE
- Now use bcinterior to find a subset of bccities
Matrices
The function matrix create a matrix from the data and parameters pass to it. This must include parameters for the number of columns and rows. The function as.matrix attempts to turn (coarse) its arguments into a matrix and is.matrix tests to see whether an argument is a matrix.
matrix(ncol = 2, nrow = 0) matrix(1:6) matrix(1:6, ncol = 2) matrix(1:6, nrow = 2) as.matrix(6:3) is.matrix(as.matrix(6:3))
Matrix rows and columns can be named.
cities.popn <- matrix(c(75150, 72406, 70981, 71974, 74003, 76708, 8468, 10044, 9326, 10007, 9879, 9889, 60186, 69217, 62927, 77936, 83788, 93203, 514008, 545671, 578041, 603502, 631486, 662248, 89442, 96288, 106707, 117312, 127380, 144576), c(5, 6), byrow = TRUE) colnames(cities.popn) <- c("1996", "2001", "2006", "2011", "2016", "2021") rownames(cities.popn) <- c("Prince George", "Quesnel", "Chilliwack", "Vancouver", "Kelowna") # examine the matrix cities.popn
Try these functions: rowSums, colSums, colMeans, rowMeans, sum, diag
- apply (x,1,max) #apply a function over rows (1) or columns(2) of x
- x[, c(TRUE,FALSE)] # logical operations to select matrix elements
Exercise:
- Lets create a 2×2 identity matrix and find its inverse.