Module 12 – Binding Rows & Columns
Module 12 delves into the art of data set restructuring in R, focusing on adding new cases (rows) and variables (columns) using the tidyverse and dplyr packages. In this module’s video lecture, you’ll learn how to combine different data sets effectively using the bind_rows()
and bind_cols()
functions. To solidify your understanding, the exercises providing practical application of these methods are included.
Video Lecture
Exercises
- Create a tibble
sleep_this_week
containing the hours of sleep you got each night during the last 7 days (feel free to make up numbers, in case you don’t remember). - Create another tibble
sleep_last_week
containing the hours of sleep you got each night the 7 days before. - Add a new column
week_no
containing the character string “week_1” repeatedly to the tibblesleep_last_week
usingbind_cols()
. Do the same forsleep_this_week
, but add the character string “week_2”. - Use
bind_rows()
to combinesleep_this_week
andsleep_last_week
into a single tibble calledcombined_sleep_data
. - Export
combined_sleep_data
to a new CSV file calledsleep-data.csv
.
The solutions to these exercises can be found at the bottom of this page.
R Code of This Lecture
# install.packages("tidyverse") # Install tidyverse packages library("tidyverse") # Load tidyverse packages vec_days <- c("Monday", "Tuesday", # Create vector of weekdays "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") vec_days # Print vector of weekdays team_coffee_Cansu <- tibble(day = vec_days, # Create tibble for Cansu cups = c(1, 0, 2, 1, 3, 0, 0)) team_coffee_Cansu # Print tibble team_coffee_Ifeanyi <- tibble(day = vec_days, # Create tibble for Ifeanyi cups = 2) team_coffee_Ifeanyi # Print tibble team_coffee_Joachim <- tibble(day = vec_days, # Create tibble for Joachim cups = c(4, 3, 2, 3, 4, 1, 1)) team_coffee_Joachim # Print tibble team_coffee_Cansu <- bind_cols(team_coffee_Cansu, # Bind columns member = c("Cansu", "Cansu", "Cansu", "Cansu", "Cansu", "Cansu", "Cansu")) team_coffee_Cansu # Print updated tibble team_coffee_Ifeanyi <- bind_cols(team_coffee_Ifeanyi, # Bind columns member = "Ifeanyi") team_coffee_Ifeanyi # Print updated tibble team_coffee_Joachim <- bind_cols(team_coffee_Joachim, # Bind columns member = "Joachim") team_coffee_Joachim # Print updated tibble team_coffee <- bind_rows(team_coffee_Cansu, # Bind rows team_coffee_Ifeanyi, team_coffee_Joachim) team_coffee %>% # Print new tibble print(n = nrow(team_coffee)) team_coffee <- team_coffee %>% # Reorder columns select(member, day, cups) team_coffee %>% # Print new tibble print(n = nrow(team_coffee)) my_path <- "D:/Dropbox/Jock/Data Sets/dplyr Course/" # Specify directory path team_coffee %>% # Export CSV file write_csv(str_c(my_path, "Team-Coffee-Data.csv"))
Exercise Solutions
Below, you can find our solutions for the exercises of this module. Before beginning the exercises, we will install and load the tidyverse
packages. The tidyverse
enables us to use the dplyr
functions.
install.packages("tidyverse") # Install tidyverse packages library(tidyverse) # Load tidyverse packages
With the tidyverse
packages loaded, we can now proceed to the solutions of the exercises.
Exercise 1: Create a tibble sleep_this_week
containing the hours of sleep you got each night during the last 7 days (feel free to make up numbers, in case you don’t remember).
sleep_this_week <- tibble(days = c("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"), # Create tibble sleep_hours = c(5,8,4,3,7,6,5)) sleep_this_week # Print to console
In the solution above, we used the tibble()
function to create a tibble sleep_this_week
containing the columns days
and sleep_hours
. The days
column contains the days of the week, while the sleep_hours
column contains the hours slept each day.
Exercise 2: Create another tibble sleep_last_week
containing the hours of sleep you got each night the 7 days before.
sleep_last_week <- tibble(days = c("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"), # Create tibble sleep_hours = c(7,4,6,5,3,8,2)) sleep_last_week # Print to console
In the above solution, we used the tibble()
function to create another tibble sleep_last_week
containing the columns days
and sleep_hours
just like in the last solution.
Exercise 3: Add a new column week_no
containing the character string “week_1” repeatedly to the tibble sleep_last_week
using bind_cols()
. Do the same for sleep_this_week
, but add the character string “week_2”.
sleep_last_week <- bind_cols(sleep_last_week, # Create new column week_no = "week_1") sleep_last_week # Print to console sleep_this_week <- bind_cols(sleep_this_week, # Create new column week_no = "week_2") sleep_this_week # Print to console
Here we used the bind_cols()
function to create new columns in both tibbles. In the tibble sleep_last_week
, we created the new column week_no
containing the character string week_1
. In the tibble sleep_this_week
, we created the new column week_no
containing the character string week_2
. In both examples, the character string of the new columns is repeated as many times as the number of rows in the tibbles.
Exercise 4: Use bind_rows()
to combine sleep_this_week
and sleep_last_week
into a single tibble called combined_sleep_data
.
combined_sleep_data <- bind_rows(sleep_this_week, # Bind tibbles rows sleep_last_week) combined_sleep_data # Print to console
In the solution above, we used the bind_rows()
function to combine sleep_this_week
and sleep_last_week
together as one tibble combined_sleep_data
.
Exercise 5: Export combined_sleep_data
to a new CSV file called sleep-data.csv
.
my_path <- "your project working directory path" # Specify directory path combined_sleep_data %>% # Export CSV file
write_csv(str_c(my_path, “sleep-Data.csv”))
Here we exported the combined_sleep_data
tibble to our project working directory using a combination of write_csv()
and str_c()
functions where we specified the project working directory path. Please ensure that your working directory path ends with “/”.
Solutions to these exercises were created in collaboration with Ifeanyi Idiaye and Cansu Kebabci. Thanks to them for their contribution!
Further Resources
- dplyr Documentation – Efficiently bind multiple data frames by row and column
- RDocumentation – bind: Efficiently bind multiple data frames by row and column
- Statistics Globe Article – Combine Two Data Frames with Different Variables by Rows in R
.
You can access the course overview page, timetable, and table of contents by clicking here.