Announcement

Install Git

Now it’s time that we try out Git, the (probably most) popular version control software. Follow instructions on installing Git from Happy Git and GitHub for the useR.

More on types

R has 5 basic or “atomic” classes of objects:

When working with vectors in R, it is important to remember that a vector can only be composed of one data type.

What happens when you put multiple objects with different type to form a vector?

The answer is coercion (in other words, type conversion).

Coercion means changing the type of an object and it happens by a pre-set order:

logical < integer < numeric < character

When you create a vector from objects with different types, the lower-order ones will be coerced to the highest type by R.

a <- c(1L, "This is a character")

b <- c(TRUE, "Hello World")

c <- c(FALSE, 2)  # what is wrong here?

Question 1 What type do the above vectors hold?

Explicit coercion

Of course you can coerce objects from one class (type) to another. We can do this by using “as.*” functions where * is the class (type) you want to coerce the object into.

# using the same objects a, b, c from the above question
a.logical <- as.logical(a)
a.integer <- as.integer(a)
## Warning: NAs introduced by coercion
a.numeric <- as.numeric(a)
## Warning: NAs introduced by coercion
b.logical <- as.logical(b)
b.integer <- as.integer(b)
## Warning: NAs introduced by coercion
b.numeric <- as.numeric(b)
## Warning: NAs introduced by coercion
c.logical <- as.logical(c)
c.integer <- as.integer(c)
c.numeric <- as.numeric(c)
c.character <- as.character(c)


d <- -5:5
d.logical <- as.logical(d)

Question 2 What do you get after the coercions? Does any one suprise you? Can you figure out why?

Hint: what are the types of each element in the vectors before you explicitly coerce them?

Try arithmetics

OK, now let’s try arithmetics.

Easy, right?

First let’s create a vector \(\mathbf{v} = (969, 971, 972, \dots, 1022, 1023)\) of 54 elements. (Attention, there is no \(970\) in the vector)

# finish the code below
v <- c()

Then, let’s compute the sum \(\sum_{i=1}^{54}2^{v_i}\).

# finish the code below
v.power.sum <- sum()

How about only sum over 53 elements \(\sum_{i=2}^{54}2^{v_i}\) (note that the sum starts from \(v_2\)).

# finish the code below
v.power.sum.53 <- sum()

Now let’s try putting the first element back

v.power.sum.second <- 2^v[1] + sum()

Question 3 Explain what you found.