Working with Vectors

Review

~
└── data-projects
    ├── project-1
       ├── fall-data.txt
       ├── spring-data.txt
       └── analysis.r
    ├── project-2
        └── summer-data.txt

Question

If your present working directory is ~/data-projects/project-2, what R command would you use to read the file named fall-data.txt?

Announcements

  • Working locally 🏆
  • Office Hours / Group Tutoring
  • Problem Sets

Vectors

  • Central data structure in R
  • Ordered set of elements
  • Atomic: elements of the same type

Atomic*

Special values in an atomic vector.

  • NA: Not available / Missing
  • NULL: Does not exist
  • NaN: Not a number (e.g. dividing by zero)
  • Inf: Infinity

A comment about comments

Comment character: special character to skip evaluation of remaining characters on a line. R (and Python) use #.

# create vector
c("A", "C", "D") # vector created!
[1] "A" "C" "D"

vs.

create vector
c("A", "C", "D") vector created!
Error in parse(text = input): <text>:1:8: unexpected symbol
1: create vector
           ^

Behavior of Vectors

Question

Turn to neighbor and speculate: what will each of the following lines of code return?

c(1, 2, 3) + c(2, 4, 8)
c(1, 2, 3) + c(2, 4)
c(1, "dog", TRUE)
02:30

Vectorization

Vectorized: Operations and functions that can run on an entire vector at once.

dbl_vec <- c(1, 2.3, 4)
log(dbl_vec) # vectorized
[1] 0.0000000 0.8329091 1.3862944
sum(dbl_vec) # vector-as-argument
[1] 7.3

Question

Turn to neighbor and speculate: what will each of the following lines of code return?

c(1, 2, 3) + c(2, 4, 8)
c(1, 2, 3) + c(2, 4)
c(1, "dog", TRUE)

Question

Turn to neighbor and speculate: what will each of the following lines of code return?

c(1, 2, 3) + c(2, 4, 8)
[1]  3  6 11
c(1, 2, 3) + c(2, 4)
c(1, "dog", TRUE)

Vector Recycling

Vector Recycling: If a function requires a longer vector than provided, it will fill the vector with the existing elements, in order.

c(2, 6, 1) ^ 2
[1]  4 36  1

Question

Turn to neighbor and speculate: what will each of the following lines of code return?

c(1, 2, 3) + c(2, 4, 8)
[1]  3  6 11
c(1, 2, 3) + c(2, 4)
c(1, "dog", TRUE)

Question

Turn to neighbor and speculate: what will each of the following lines of code return?

c(1, 2, 3) + c(2, 4, 8)
[1]  3  6 11
c(1, 2, 3) + c(2, 4)
[1] 3 6 5
c(1, "dog", TRUE)

Type Coersion

Implicit Coersion: Automatic change of data types to ensure atomic vectors. Changes follow a hierarchy.

c(1, 2, "three")
[1] "1"     "2"     "three"

Note: logical vectors can be coerced to integers for mathematical operations

Question

Turn to neighbor and speculate: what will each of the following lines of code return?

c(1, 2, 3) + c(2, 4, 8)
[1]  3  6 11
c(1, 2, 3) + c(2, 4)
[1] 3 6 5
c(1, "dog", TRUE)

Question

Turn to neighbor and speculate: what will each of the following lines of code return?

c(1, 2, 3) + c(2, 4, 8)
[1]  3  6 11
c(1, 2, 3) + c(2, 4)
[1] 3 6 5
c(1, "dog", TRUE)
[1] "1"    "dog"  "TRUE"

Type Coersion

Explicit Coersion

as.__(x): sets x to be of type ___

  • as.double()
  • as.integer()
  • as.character()
  • as.logical()
as.double(c(TRUE, FALSE, FALSE, FALSE))
[1] 1 0 0 0

Vector Subsetting

Square Bracket Syntax

Subsetting by Index

char_vec <- c("cat", "dog", "dingo")
char_vec
[1] "cat"   "dog"   "dingo"
char_vec[2]
[1] "dog"
char_vec[2:3]
[1] "dog"   "dingo"

Note: the indexing vector is not recycled.

Subsetting by Name

named_char_vec <- c("iz" = "cat", "buzz" = "dog", "poppy" = "dingo")
named_char_vec
     iz    buzz   poppy 
  "cat"   "dog" "dingo" 
named_char_vec["buzz"]
 buzz 
"dog" 

Subsetting by Logicals

named_char_vec[c(TRUE, FALSE, TRUE)]
     iz   poppy 
  "cat" "dingo" 

Note: the logical vector should be of the same length.

Question

Write down three methods to generate each vector from the vector z, one for each subsetting method.

z <- c(apple = 2, banana = 4, cherry = 8)

Vector 1:

banana  apple 
     4      2 

Vector 2:

cherry cherry 
     8      8 
02:30

Change in place syntax

You can assign into a subsetted vector to change its contents in place.

z <- c(apple = 2, banana = 4, cherry = 8)
z[c(TRUE, FALSE, TRUE)] <- 12
z
 apple banana cherry 
    12      4     12 
names(z)
[1] "apple"  "banana" "cherry"
names(z) <- c("ali", "bob", "chip")
z
 ali  bob chip 
  12    4   12 

For next time

  • Work on problem sets and lab.
  • Visit office hours and group tutoring.
  • Have a great weekend 😎