R Academy · Lesson

Combining and Reshaping Data

Explore merging, appending, and reshaping data sets for flexible analysis.

Lesson 3 of 310 steps

Combining and Reshaping Data is a free R Academy lesson on CoddyKit. This is lesson 3 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the R Academy learning path, and your progress syncs across the web and the CoddyKit app. The R Academy course includes 3 lessons in total.

Introduction to Combining and Reshaping Data

Data often comes from multiple sources and needs to be merged or reshaped for analysis. In this lesson, you will learn how to combine and restructure data in R.

Combining and Reshaping Data — illustration 1

Combining Data Frames

You can combine data frames using functions like rbind() (row-wise) and cbind() (column-wise).

df1 <- data.frame(ID = c(1, 2), Score = c(90, 85))
df2 <- data.frame(ID = c(3, 4), Score = c(78, 92))
df_combined <- rbind(df1, df2)

Merging Data Frames

To merge two data frames by a common column, use merge().

df1 <- data.frame(ID = c(1, 2, 3), Name = c('Alice', 'Bob', 'Charlie'))
df2 <- data.frame(ID = c(1, 2, 3), Score = c(90, 85, 88))
merged_df <- merge(df1, df2, by = 'ID')

Using dplyr for Joins

The dplyr package provides functions like left_join() and inner_join() for merging data.

library(dplyr)
joined_df <- left_join(df1, df2, by = 'ID')

Pivoting Data with tidyr

Reshaping data is useful for summarizing and analyzing different formats.

Widening Data (pivot_wider)

Use pivot_wider() to convert long format data to wide format.

library(tidyr)
data_wide <- pivot_wider(df, names_from = Category, values_from = Value)

Longer Data (pivot_longer)

Use pivot_longer() to convert wide format data to long format.

data_long <- pivot_longer(df, cols = c('Jan', 'Feb'), names_to = 'Month', values_to = 'Sales')

Sorting Data

You can sort data frames using the arrange() function from dplyr.

df_sorted <- arrange(df, desc(Score))

Summary

In this lesson, you learned:

  • How to combine and merge data frames.
  • How to reshape data using pivoting functions.
  • How to sort and structure data efficiently.
Combining and Reshaping Data — illustration 10
Free to start

Learn R with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
43
Lessons
159

Frequently Asked Questions

Is the “Combining and Reshaping Data” lesson free?

Yes — any 3 lessons of the R Academy learning path, including “Combining and Reshaping Data”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The R Academy course includes 3 lessons in total.

What will I learn in “Combining and Reshaping Data”?

Explore merging, appending, and reshaping data sets for flexible analysis. You practise R Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start R Academy?

No prior experience is required. R Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.

How long does the “Combining and Reshaping Data” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this R Academy lesson?

Yes. Every R Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Importing Data
  2. Data Cleaning Basics
  3. Combining and Reshaping Data
← Back to R Academy