Indexing

On Thursday Problem Set 2 was distributed. A digital version of the prompt can be accessed here: ProblemSet2.pdf. The highlights from this week are summarized below:

Indexing

To extract individual elements or sub-vectors from a vector the [] notation can be used. Numbers (integers) supplied inside of the [] will instruct R to extract the objects located at the corresponding position inside of the vector.

# extracts the element at position 26 of the vector letters
# will return "z"

letters[26] 

# will return "h"

letters[8]

# will return vector containing "h" and "z"

letters[c(8, 26)]

# will return vector containing "a", "b", and "c" 

letters[1:3]

# will return letter "i"

letters[8+1] 

# will return "d"

Fourth_letter <- 4

letters[Fourth_letter]

# will return vector of length 24 containing 
# all letters except "a" and "h"

letters[-c(1,8)]

# assignes the first three letters to a 
# new vector called x

x <- letters[1:3]

# replaces the second element of vector x
# with the word "Hello"

x[2] <- "Hello"

We also discussed the which() function which returns a vector of index locations meeting a logical condition and the subset() function which can be used to extract elements meeting a certain condition.

# evaluates to 8 and 17

which(letters == "h" | letters == "q")

# extract letters h and q

subset(x = letters, letters == "h" | letters == "q")

Indexing w/ TRUE and FALSE

Among the things covered was the use of TRUE and FALSE inside of the [] brackets to extract elements from vectors.

abc <- c("a", "b", "c", "d", "e")

# returns a vector containing all the elements of abc

abc[c(TRUE, TRUE, TRUE, TRUE, TRUE)]

# returns a vector that is empty (character(0))

abc[c(FALSE, FALSE, FALSE, FALSE, FALSE)]

# the empty vector is of type character

typeof(x = abc[c(FALSE, FALSE, FALSE, FALSE, FALSE)])

# the empty vector is indeed empty and has length 0

length(x = abc[c(FALSE, FALSE, FALSE, FALSE, FALSE)])

# returns a vector containing only the second and forth element of abc
# that is "b" and "d"

abc[c(FALSE, TRUE, FALSE, TRUE, FALSE)]

# creates an empty vector of type double

foo <- c(pi, -0.5)[c(FALSE, FALSE)]

# as does this

foobar <- pi[FALSE]

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 "z"

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(path = "C:/Users/UserName/Desktop") # windows

list.files(path = "/Users/Username/Desktop") # mac

# the working directory can be changed via

setwd(dir = "C:/Users/UserName/Desktop") # windows

setwd(dir = "/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")