More Indexing and Matresses07 Feb 2018
Indexing w/ TRUE and FALSE
We began with a discussion of using TRUE and FALSE inside of the
 brackets to extract elements from vectors.
abc <- c("a", "b", "c") # returns the first and third element of abc # i.e., those positions that are set to TRUE abc[c(TRUE, FALSE, TRUE)]
With this in mind, we can use logical arguments to subset vectors.
# returns a vector of length 26 # containing the logical value FALSE 24 times and # the logical value TRUE 2 times (in the first and # last position) letters == "a" | letters == "z" # returns the letters "a" and "b" letters[letters == "a" | letters == "z"]
Managing Objects and Data
R is perfectly capable of serving as a file system browser.
# the present working directory can be identified via getwd() # files and folders present in the current working # directory can be identified via list.files() # files and folders present in an arbitrary # directory can be identified by specifying the path # for example: list.files("C:/Users/UserName/Desktop") # windows list.files("/Users/Username/Desktop") # mac # the working directory can be changed via setwd("C:/Users/UserName/Desktop") # windows setwd("/Users/Username/Desktop") # mac # saving objects stored in R's active memory # in the current working directory (with the .Rdata extension) x <- rnorm(n = 100, mean = 0, sd = 10) y <- runif(n = 55, min = 0, max = 1) save(x, y, file = "my-data.RData") # removes all objects in R's active memory rm(list = ls()) # load the objects x and y back into R's active memory # load(file = "my-data.RData")
We barely scratched the surface with matrices. We discussed three functions to generate matrices:
x <- 1:10 y <- 10:1 # binds the vectors x and y together (as rows) # returns a 2 by 10 matrix rbind(x, y) # binds the vectors x and y together (as columns) # returns a 10 by 2 matrix cbind(x, y) # returns a 5 by 5 matrix containing 0's matrix(data = 0, nrow = 5, ncol = 5) # returns a 4 by 10 matrix containing a # sequence of integers from 1 to 40, filled by row matrix(data = 1:40, nrow = 4, ncol = 10, byrow = TRUE)