Practice: Data Tables

This is the final practice of data frame formats. Now you should be familiar with the three main ones: data.frame, data.table, and tibble. Repeat the practice tasks for tibbles now using the data.table format instead. This exercise does not count toward your grade. It is just for practice!

Exercises

  1. How can you tell if an object is a tibble? (Hint: try printing mtcars, which is a regular data frame).

  2. Compare and contrast the following operations on a data.frame and equivalent tibble. What is different? Why might the default data frame behaviours cause you frustration?

    df <- data.frame(abc = 1, xyz = "a")
    df$x
    df[, "xyz"]
    df[, c("abc", "xyz")]
  3. If you have the name of a variable stored in an object, e.g. var <- "mpg", how can you extract the reference variable from a tibble?

  4. Practice referring to non-syntactic names in the following data frame by:

    1. Extracting the variable called 1.

    2. Plotting a scatterplot of 1 vs 2.

    3. Creating a new column called 3 which is 2 divided by 1.

    4. Renaming the columns to one, two and three.

    annoying <- tibble(
      `1` = 1:10,
      `2` = `1` * 2 + rnorm(length(`1`))
    )
  5. What does tibble::enframe() do? When might you use it?

  6. What option controls how many additional column names are printed at the footer of a tibble?


Source: H. Wickham and G. Grolemund, https://r4ds.had.co.nz/tibbles.html
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 3.0 License.

Last modified: Thursday, December 15, 2022, 4:44 PM