Problem 2: Importing data

heights_df <- read.csv("heights.csv")
sum(heights_df$height)
## [1] 86427

Problem 3: String concatenation

print("hello world")
## [1] "hello world"
paste("hello", "world") # concatenate the strings with a space in between
## [1] "hello world"
paste0("hello", "world") # no space in between
## [1] "helloworld"

Problem 4: Vector coercion

x <- 1:10 # integer vector
x[5] <- "cat"
x # now it is a character vector

So the numeric values were coerced to strings.

Let’s try with logicals:

x <- c(T, T, F, F) # Logical vector
x[3] <- 99
x # Now it is an integer vector. T and F became their numeric values
## [1]  1  1 99  0
x[2] <- "pig"
x # Now it is a character vector
## [1] "1"   "pig" "99"  "0"

So the coercion hierarchy is character -> numeric -> logical.

The following is amusing:

x <- c(T, T, F, F) # Logical vector
x[2] <- "chicken"
x
## [1] "TRUE"    "chicken" "FALSE"   "FALSE"

Here the logicals are converted to the strings “TRUE” and “FALSE”.