Melting and Casting in R Programming

12 Jun, 2020
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 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 reshapedna.rm: if TRUE, removes NA values from datasetvalue.name: represents name of variable used to store values
Example:
# Required library for ships datasetinstall.packages("MASS")  # Required for melt() and cast() functioninstall.packages("reshape2")install.packages("reshape")  #Loading the librarieslibrary(MASS)library(reshape2)library(reshape)  # Create dataframen <- 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 framecat("Original data frame:\n")print(df)  # Organize data w.r.t. n and timemolten.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 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 datasetformula: represents the form in which data has to be reshapedfun.aggregate: represents aggregate function
Example:
# Print recasted dataset using cast() functioncast.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

Picked
R-DataFrame
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Filter data by multiple conditions in R using Dplyr
Change Color of Bars in Barchart using ggplot2 in R
Loops in R (for, while, repeat)
How to Split Column Into Multiple Columns in R DataFrame?
Group by function in R using Dplyr
How to change Row Names of DataFrame in R ?
How to Change Axis Scales in R Plots?
How to filter R DataFrame by values in a column?
Logistic Regression in R Programming
R - if statement