R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Tibbles are a modern reimagining of data frames in R and come as part of the tidyverse
suite of packages. Tibbles are similar to data frames but have some notable differences that make them more user-friendly, especially during the data wrangling process.
install.packages("tidyverse") library(tidyverse)
df <- data.frame(x = 1:5, y = letters[1:5]) tb <- as_tibble(df)
tibble()
:tb <- tibble(x = 1:5, y = letters[1:5])
Printing: Tibbles don't print too many rows by default, making it easier to view in the console.
Subsetting: With a tibble, if you subset with square brackets and only select one column, it remains a tibble. With a data frame, it becomes a vector.
Column Data Types: Tibbles are less strict when creating columns of different data types.
Using the $
operator:
tb$y
Using the double square bracket:
tb[[2]]
tb <- tb %>% mutate(z = x * 2)
tb <- tb %>% rename(new_x = x)
tb <- tb %>% select(-new_x)
Tibbles provide easy access to their metadata:
glimpse(tb)
nrow(tb)
ncol(tb)
You can use add_row()
to add new rows:
tb <- add_row(tb, x = 6, y = "f", z = 12)
tb <- tb %>% filter(x != 6)
enframe()
: Converts a named vector into a two-column tibble.vec <- c(a = 1, b = 2, c = 3) enframe(vec)
deframe()
: Converts a two-column tibble into a named vector.deframe(enframe(vec))
In case you need to convert a tibble back to a regular data frame:
df <- as.data.frame(tb)
Tibbles are an essential tool for data wrangling in the modern R ecosystem, offering various advantages over traditional data frames, especially in terms of usability. As you explore the tidyverse
, you'll find that many functions return tibbles by default, making it a valuable structure to understand and use effectively.
Tibble vs data.frame in R:
# Creating a data.frame df <- data.frame(ID = 1:3, Name = c("Alice", "Bob", "Charlie")) # Creating a tibble library(tibble) tbl <- tibble(ID = 1:3, Name = c("Alice", "Bob", "Charlie"))
Data wrangling with tibbles examples:
# Using dplyr and tibble for data wrangling library(dplyr) library(tibble) df <- data.frame(ID = 1:3, Name = c("Alice", "Bob", "Charlie")) tbl <- as_tibble(df) # Data wrangling with tibbles wrangled_tbl <- tbl %>% filter(ID > 1) %>% mutate(NewColumn = nchar(Name))
R tibble functions and operations:
# Using tibble functions library(tibble) tbl <- tibble(ID = 1:3, Name = c("Alice", "Bob", "Charlie")) # Adding a new column tbl <- add_column(tbl, NewColumn = c(10, 20, 30)) # Selecting specific columns selected_tbl <- select(tbl, ID, Name)
Introduction to tibbles in R:
# Creating a tibble library(tibble) tbl <- tibble(ID = 1:3, Name = c("Alice", "Bob", "Charlie"))
Using tibbles for data manipulation in R:
# Using tibbles with dplyr library(dplyr) library(tibble) tbl <- tibble(ID = 1:3, Name = c("Alice", "Bob", "Charlie")) # Data manipulation with dplyr and tibbles manipulated_tbl <- tbl %>% filter(ID > 1) %>% mutate(NewColumn = nchar(Name))
Converting data.frame to tibble in R:
as_tibble
function.# Converting data.frame to tibble library(tibble) df <- data.frame(ID = 1:3, Name = c("Alice", "Bob", "Charlie")) tbl <- as_tibble(df)
R tidyverse and tibble usage:
# Using tibbles within the tidyverse library(tidyverse) tbl <- tibble(ID = 1:3, Name = c("Alice", "Bob", "Charlie")) # Data wrangling with tidyverse wrangled_tbl <- tbl %>% filter(ID > 1) %>% mutate(NewColumn = nchar(Name))