# SETUP: Install and load the tidyverse package
# Extras pane > Packages tab > Install
library(tidyverse)
# ==============================================================================
# LESSON: Create a tibble from vectors
x <- c(10, 20, 30, 40)
x
y <- x * 2 - 4
y
my_tibble <- tibble(x, y)
my_tibble
# ==============================================================================
# USECASE: You can mix different types of vectors in a single tibble
first_names <- c("Adam", "Billy", "Caitlyn", "Debra")
age_years <- c(12, 13, 10, NA)
guests <- tibble(first_names, age_years)
guests
# ==============================================================================
# TIP: To save time, you can also create the vectors in the tibble call
gradebook <- tibble(
grade = c(95, 83, 90, 76),
letter = c("a", "b", "a-", "c")
)
gradebook
# ==============================================================================
# PITFALL: Don't try to combine tibbles with different lengths
y <- c(1, 2, 3)
x <- c("a", "b")
tibble(y, x) #error
# ==============================================================================
# LESSON: You can "extract" a vector from a tibble using $
mytibble <- tibble(x = c(1, 2, 3, 4, 5), y = "test")
mytibble$x
mytibble$y
# ==============================================================================
# PITFALL: Don't try to extract a vector that doesn't exist
mytibble$z #error
# ==============================================================================
# USECASE: Get information about a tibble
dim(gradebook)
colnames(gradebook)