Practice: Tibbles

Try working with tibbles in these exercises. Remember, tibbles are just another form of the data.frame format. Do you find tibbles more convenient to use? 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: Friday, December 16, 2022, 11:57 AM