Open In App

Melting and Casting in R Programming

Improve
Improve
Like Article
Like
Save
Share
Report

Melting and Casting are one of the interesting aspects in R programming to change the shape of the data and further, getting the desired shape. R programming language has many methods to reshape the data using reshape package. melt() and cast() are the functions that efficiently reshape the data. There are many packages in R that require data reshaping. Each data is specified in multiple rows of dataframe with different details in each row and this type of format of data is known as long format.

Melting in R

Melting in R programming is done to organize the data. It is performed using melt() function which takes dataset and column values that has to be kept constant. Using melt(), dataframe is converted into long format and stretches the data frame.

Syntax:
melt(data, na.rm = FALSE, value.name = “value”)

Parameters:
data: represents dataset that has to be reshaped
na.rm: if TRUE, removes NA values from dataset
value.name: represents name of variable used to store values

Example:




# Required library for ships dataset
install.packages("MASS")
  
# Required for melt() and cast() function
install.packages("reshape2")
install.packages("reshape")
  
#Loading the libraries
library(MASS)
library(reshape2)
library(reshape)
  
# Create dataframe
n <- c(1, 1, 2, 2)
time <- c(1, 2, 1, 2)
x <- c(6, 3, 2, 5)
y <- c(1, 4, 6, 9)
df <- data.frame(n, time, x, y)
  
# Original data frame
cat("Original data frame:\n")
print(df)
  
# Organize data w.r.t. n and time
molten.data <- melt(df, id = c("n","time"))
  
cat("\nAfter melting data frame:\n")
print(molten.data)


Output:

Original data frame:
  n time x y
1 1    1 6 1
2 1    2 3 4
3 2    1 2 6
4 2    2 5 9


After melting data frame:
  n time variable value
1 1    1        x     6
2 1    2        x     3
3 2    1        x     2
4 2    2        x     5
5 1    1        y     1
6 1    2        y     4
7 2    1        y     6
8 2    2        y     9

Casting in R

Casting in R programming is used to reshape the molten data using cast() function which takes aggregate function and formula to aggregate the data accordingly. This function is used to convert long format data back into some aggregated form of data based on the formula in the cast() function.

Syntax:
cast(data, formula, fun.aggregate)

Parameters:
data: represents dataset
formula: represents the form in which data has to be reshaped
fun.aggregate: represents aggregate function

Example:




# Print recasted dataset using cast() function
cast.data <- cast(molten.data, n~variable, sum)
  
print(cast.data)
  
cat("\n")
time.cast <- cast(molten.data, time~variable, mean)
print(time.cast)


Output:

  n x  y
1 1 9  5
2 2 7 15

  time x   y
1    1 4 3.5
2    2 4 6.5


Last Updated : 12 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads