🗓️ Session 3: Basic object types in R

Author
Published

28 03 2024

Modified

18 04 2024

This session is video-based since it introduces some very important fundamentals, which might be a bit boring to listen to in a lecture. In the videos, you learn about the most important and most fundamental object types in R, such as decimal numbers or words. While this might look a bit boring at first, understanding these basic types is fundamental for all the more advanced (and exiting) stuff in the future!

👨‍🏫 Lecture Slides

Either click on the slide area below or click here to download the slides.

# Task 1: Intermediate exercises I===============
# Create a vector containing the numbers 2, 5, 2.4 and 11.
vec_task_1 <- c(2, 5, 2.4, 11)
# What is the type of this vector?
typeof(vec_task_1)
# Transform this vector into the type integer. What happens?
typeof(vec_task_1)
as.integer(vec_task_1) # Does not modify the original vector
typeof(vec_task_1)
vec_task_1_int <- as.integer(vec_task_1) # Modifies the original vector
typeof(vec_task_1_int)
# Do you think you can create a vector containing the following elements:
# "2", "Hello", 4.0, and TRUE?
mixed_vec <- c("2", "Hello", 4.0, TRUE)
mixed_vec
# See the tutorial for an explanation of the underlying hierarchy and
# why the type is as follows:
typeof(mixed_vec)
# Task 2: Intermediate exercises II==============
# Create a vector with the numbers from -2 to 19 (step size: 0.75)
seq_vec <- seq(-2, 19, by=0.75)
seq_vec
# Create an index vector for this first vector (note: an index vector is a
# vector with all possible indices of the original vector)
seq_vec_index <- seq_along(seq_vec)
seq_vec_index
# Compute the log of each element of the first vector using vectorisation.
# Anything that draws your attention?
log(seq_vec)
# Logs for negative values do not exist, so NaNs were added
# What happens if you concatenate vectors of different types using c()?
# Can you derive a systematization?
v_double <- c(2.0, 3.8)
v_int <- c(1L, 3L)
v_char <- c("1", "99")
typeof(c(v_double, v_int))
typeof(c(v_char, v_int))
# The concatenated vector is coerced into the type of the original vector
# according to the standard type hierarchy
# Task 3: Intermediate exercises III=============
# Create a list that has three named elements: "A", "B", and "C"
# The element "A" should contain the square root of the
# numbers form -2 to 8 (step size: 1)
# The element "B" should contain the log of numbers between 2 and 4
# (step size: 0.5)
# The element "C" should contain letters from a1 to g7
# (hint: use the pre-defined vector letters and the function paste())
li_1 <- list(
"A"=sqrt(seq(-2, 8, by=1)),
"B"=log(seq(2, 4, by=0.5)),
"C"=paste(letters[1:7], 1:7, sep = "")
)
li_1
# Recommendable:
a_element <- sqrt(seq(-2, 8, by=1))
b_element <- log(seq(2, 4, by=0.5))
c_element <- paste(letters[1:7], 1:7, sep = "")
li_1 <- list(
"A"=a_element,
"B"=b_element,
"C"=c_element
)
li_1

🎥 Lecture videos

All the videos are available via this playlist.

📚 Mandatory Reading

Read the following tutorials:

🏆 Further readings

I suggest you read these references after you learned about data frames in session 4 and data wrangling techniques in sessions 8 and 9. - Sections 1-3 in Chapter 12 of Wickham et al. (). - Chapter 13 in Wickham et al. (). - Chapter 14 in Wickham et al. ().

✍️ Coursework

  • Do the ObjectTypes1 exercises of the package DataScienceExercises
learnr::run_tutorial(
  name = "ObjectTypes1", 
  package = "DataScienceExercises", 
  shiny_args=list("launch.browser"=TRUE))
  • If you have questions or problems, please post them in the Moodle forum

References

Wickham, H., Çetinkaya-Rundel, M. and Grolemund, G. (2023) R for data science: Import, tidy, transform, visualize, and model data, 2nd edition., Beijing et al.: O’Reilly, available at https://r4ds.hadley.nz/.