Open In App

Data Reshaping in R Programming

Last Updated : 12 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In R programming, data processing typically involves working with data organized into rows and columns within a data frame. Data frames are widely used because extracting data from them is simple. However, sometimes we need to reshape the format of a data frame to suit our analysis. In R, we can reshape data frames using various functions such as splitting, merging, and transposing.

The main methods of reshaping data in a data frame are

  1. Transpose of a Matrix
  2. Joining Rows and Columns
  3. Merging of Data Frames
  4. Melting and Casting

Why Data Reshaping is Important in R

Data reshaping is important in R because the structure of data may vary depending on the experiment or study. The data contains one or more columns identifying each row, followed by columns representing measured values. Reshaping the data helps organize it according to the needs of analysis.

1. Transpose of a Matrix

We can easily calculate the transpose of a matrix in R language with the help of the t() function. The t() function takes a matrix or data frame as an input and gives the transpose of that matrix or data frame as its output.

Syntax: 

t(Matrix/ Data frame)

Where: :

  • t(): Used to transpose a matrix or data frame, switching its rows and columns.

Example: We transpose a matrix using the t() function to switch rows and columns.

R
first <- matrix(c(1:12), nrow=4, byrow=TRUE)
print("Original Matrix")
first

first <- t(first)
print("Transpose of the Matrix")
first

Output:

matrix
Output

2. Joining Rows and Columns in Data Frame

We use the cbind() and rbind() functions to join vectors, matrices, or data frames by columns or rows.

cbind(): We can combine vectors, matrix or data frames by columns using cbind() function.

Syntax:

cbind(x1, x2, x3)

Where:

  • x1, x2 and x3 can be vectors or matrices or data frames. 

rbind(): We can combine vectors, matrix or data frames by rows using rbind() function.

rbind(x1, x2, x3)

Where:

  • x1, x2 and x3 can be vectors or matrices or data frames.

Example: We join vectors into a data frame using cbind() and rbind() to combine the data frames.

R
name <- c("Shaoni", "esha", "soumitra", "soumi")
age <- c(24, 53, 62, 29)
address <- c("puducherry", "kolkata", "delhi", "bangalore")

info <- cbind(name, age, address)
print("Combining vectors into data frame using cbind ")
print(info)

newd <- data.frame(name=c("sounak", "bhabani"),
                   age=c("28", "87"),
                   address=c("bangalore", "kolkata"))

new.info <- rbind(info, newd)
print("Combining data frames using rbind ")
print(new.info)

Output:

data
Output

3. Merging Two Data Frames

We merge two data frames using the merge() function. Both data frames must have the same column names to merge correctly.

Syntax:

merge(dfA, dfB, ...)

Where:

  • merge(): Combines two data frames based on a matching key or column.

Example: We merge two data frames using merge().

R
d1 <- data.frame(name=c("shaoni", "soumi", "arjun"),
                 ID=c("111", "112", "113"))

d2 <- data.frame(name=c("sounak", "esha"),
                 ID=c("114", "115"))

total <- merge(d1, d2, all=TRUE)
print(total)

Output:

data
Output

4. Melting and Casting

We use melt() to convert a data frame into a molten form and dcast() to aggregate it into a new format.

1. melt(): It is used to convert a data frame into a molten data frame.

Syntax:

melt(data, ..., na.rm=FALSE, value.name="value")

Where:

  • data: data to be melted 
  • ... : arguments 
  • na.rm: converts explicit missings into implicit missings 
  • value.name: storing values

2. dcast(): It is used to aggregate the molten data frame into a new form.

Syntax:

melt(data, formula, fun.aggregate)

Where:

  • data: data to be melted 
  • formula: formula that defines how to cast 
  • fun.aggregate: used if there is a data aggregation

Example: We melt the data to get a molten format and then cast it into a new structure.

R
install.packages("reshape2")
library(reshape2)

library(reshape2)

a <- data.frame(id = c("1", "1", "2", "2"),
                points = c("1", "2", "1", "2"),
                x1 = c("5", "3", "6", "2"),
                x2 = c("6", "5", "1", "4"))

a$x1 <- as.numeric(as.character(a$x1))
a$x2 <- as.numeric(as.character(a$x2))

print("Melting")
m <- melt(a, id = c("id", "points"))
print(m)

print("Casting")
idmn <- dcast(m, id ~ variable, mean)
print(idmn)

Output:

data
Output

The output shows the process of melting data to create a long-format version of the original dataset and then casting it to compute the mean of each variable (x1, x2) for each id. This reshapes the data, making it more suitable for analysis or visualization.


Similar Reads