Data Reshaping in R Programming

Last Updated : 01 Aug, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Generally, in R Programming Language, data processing is done by taking data as input from a data frame where the data is organized into rows and columns. Data frames are mostly used since extracting data is much simpler and hence easier. But sometimes we need to reshape the format of the data frame from the one we receive. Hence, in R, we can split, merge and reshape the data frame using various functions.

The various forms of reshaping data in a data frame are:

  • Transpose of a Matrix
  • Joining Rows and Columns
  • Merging of Data Frames
  • Melting and Casting

Why R – Data Reshaping is Important?

While doing an analysis or using an analytic function, the resultant data obtained because of the experiment or study is generally different. The obtained data usually has one or more columns that correspond or identify a row followed by a number of columns that represent the measured values. We can say that these columns that identify a row can be the composite key of a column in a database.

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)

Example:

R




# R program to find the transpose of a matrix
 
first <- matrix(c(1:12), nrow=4, byrow=TRUE)
print("Original Matrix")
first
 
first <- t(first)
print("Transpose of the Matrix")
first


Output:

[1] "Original Matrix"
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12

[1] "Transpose of the Matrix"

[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12

Joining Rows and Columns in Data Frame

In R, we can join two vectors or merge two data frames using functions. There are basically two functions that perform these tasks:

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.

Syntax: rbind(x1, x2, x3)

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

Example:

R




# Cbind and Rbind function in R
name <- c("Shaoni", "esha", "soumitra", "soumi")
age <- c(24, 53, 62, 29)
address <- c("puducherry", "kolkata", "delhi", "bangalore")
 
# Cbind function
info <- cbind(name, age, address)
print("Combining vectors into data frame using cbind ")
print(info)
 
# creating new data frame
newd <- data.frame(name=c("sounak", "bhabani"),
                   age=c("28", "87"),
                   address=c("bangalore", "kolkata"))
 
# Rbind function
new.info <- rbind(info, newd)
print("Combining data frames using rbind ")
print(new.info)


Output:

[1] "Combining vectors into data frame using cbind "
name age address
[1,] "Shaoni" "24" "puducherry"
[2,] "esha" "53" "kolkata"
[3,] "soumitra" "62" "delhi"
[4,] "soumi" "29" "bangalore"

[1] "Combining data frames using rbind "

name age address
1 Shaoni 24 puducherry
2 esha 53 kolkata
3 soumitra 62 delhi
4 soumi 29 bangalore
5 sounak 28 bangalore
6 bhabani 87 kolkata

Merging two Data Frames

In R, we can merge two data frames using the merge() function provided both the data frames should have the same column names. We may merge the two data frames based on a key value.

Syntax: merge(dfA, dfB, …)

Example:

R




# Merging two data frames in 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:

    name  ID
1 arjun 113
2 shaoni 111
3 soumi 112
4 esha 115
5 sounak 114

Melting and Casting

Data reshaping involves many steps in order to obtain desired or required format. One of the popular methods is melting the data which converts each row into a unique id-variable combination and then casting it. The two functions used for this process:

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

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:

R




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"))
 
# Convert numeric columns to actual numeric values
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:

[1] "Melting" 

id points variable value
1 1 1 x1 5
2 1 2 x1 3
3 2 1 x1 6
4 2 2 x1 2
5 1 1 x2 6
6 1 2 x2 5
7 2 1 x2 1
8 2 2 x2 4

[1] "Casting"

id x1 x2
1 1 4 5.5
2 2 4 2.5


Similar Reads

Reshaping data.frame from wide to long format in R
Reshaping a data frame from wide to long format in R Programming Language is a common operation when dealing with data analysis and visualization. The process involves converting data that is spread across multiple columns (wide format) into a format where each row represents a single observation (long format). This article covers various methods t
4 min read
Data Wrangling in R Programming - Data Transformation
A dataset can be presented in many different ways to the world. Let us look at one of the most essential and fundamental distinctions, whether a dataset is wide or long. The difference between wide and long datasets condenses to whether we prefer to have more rows in our dataset or more columns. A dataset that spotlights on putting additional data
3 min read
Convert an Object to Data Frame in R Programming - as.data.frame() Function
as.data.frame() function in R Programming Language is used to convert an object to data frame. These objects can be Vectors, Lists, Matrices, and Factors. Syntax: as.data.frame(object) Parameters: object: Vector, Matrix, factor, or data frameR - as.data.frame() Function ExampleExample 1: Basic example of as.data.frame() Function in R C/C++ Code # R
2 min read
Check if the Object is a Data Frame in R Programming - is.data.frame() Function
is.data.frame() function in R Language is used to return TRUE if the specified data type is a data frame else return FALSE. R data.frame is a powerful data type, especially when processing table (.csv). It can store the data as row and columns according to the table. Syntax: is.data.frame(x) Parameters: x: specified data.frame Example 1: # R progra
1 min read
Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function
data.matrix() function in R Language is used to create a matrix by converting all the values of a Data Frame into numeric mode and then binding them as a matrix. Syntax: data.matrix(df) Parameters: df: Data frame to be converted. Example 1: # R program to convert a data frame # into a numeric matrix # Creating a dataframe df1 = data.frame( "Na
2 min read
Modify Data of a Data Frame with an Expression in R Programming - with() Function
with() function in R Language is used to modify data of a data frame by evaluating an expression within the arguments of the function. Syntax: with(x, expr) Parameters: x: Data frame expr: Expression to modify data Example 1: # R program to modify data of an object # Calling predefined data set BOD # Calling with() function with(BOD, {BOD$demand
1 min read
Generate a set of Sample data from a Data set in R Programming - sample() Function
sample() function in R Language creates random sample based on the parameters provided in the function call. It takes either a vector or a positive integer as the object in the function parameter. Syntax: sample(x, size, replace) Parameters: x: indicates either vector or a positive integer or data frame size: indicates size of sample to be taken re
2 min read
data.table vs data.frame in R Programming
data.table in R is an enhanced version of the data.frame. Due to its speed of execution and the less code to type it became popular in R. The purpose of data.table is to create tabular data same as a data frame but the syntax varies. In the below example let we can see the syntax for the data table: C/C++ Code DataTable = data.table(name = c("
3 min read
Is it Mandatory to Learn R Programming Language for Becoming a Master in Data Science and Big Data?
It is not mandatory to learn R programming to become a master in data science and big data, but it is highly beneficial. R is one of the leading languages for statistical analysis and has powerful tools for data visualization, which are crucial skills in data science. However, proficiency in other languages like Python can also provide a comprehens
3 min read
What is the Relation Between R Programming and Hadoop Programming?
The relation between R programming and Hadoop revolves around integrating R with the Hadoop ecosystem to analyze large datasets that are stored in a Hadoop environment. R, primarily used for statistical analysis and data visualization, is not inherently built for handling big data. However, when combined with Hadoop, it can leverage Hadoop's distri
3 min read
Data Handling in R Programming
R Programming Language is used for statistics and data analytics purposes. Importing and exporting of data is often used in all these applications of R programming. R language has the ability to read different types of files such as comma-separated values (CSV) files, text files, excel sheets and files, SPSS files, SAS files, etc. R allows its user
5 min read
Reading Tabular Data from files in R Programming
Often, the data which is to be read and worked upon is already stored in a file but is present outside the R environment. Hence, importing data into R is a mandatory task in such circumstances. The formats which are supported by R are CSV, JSON, Excel, Text, XML, etc. The majority of times, the data to be read into R is in tabular format. The funct
4 min read
Comparing values of data frames in R Programming - all_equal() Function
all_equal() function in R Language is used to compare the values between dataframes. Syntax: all_equal(target, current, check.attributes, check.names) Parameters: target: object to comparecurrent: object to be compared withcheck.attributes: If attributes of both be comparedcheck.names: If names be comparedR - compare values of data frames Example 1
2 min read
Convert type of data object in R Programming - type.convert() Function
type.convert() function in R Language is used to compute the data type of a particular data object. It can convert data object to logical, integer, numeric, or factor. Syntax: type.convert(x) Parameter: x: It can be a vector matrix or an array Example 1: Apply type.convert to vector of numbers # R program to illustrate # type.convert to vector of n
2 min read
Create Matrix and Data Frame from Lists in R Programming
In R programming, there 5 basic objects. Lists are the objects that can contain heterogeneous types of elements, unlike vectors. Matrices can contain the same type of elements or homogeneous elements. On the other hand, data frames are similar to matrices but have an advantage over matrices to keep heterogeneous elements. In this article, we'll lea
3 min read
Check if Object is of the Character Data type in R Programming - is.character() Function
is.character() Function in R Language is used to check if the object is of the form of a string/character or not. It will return true if any element of the object is of the character data type. Syntax: is.character(Object) Parameter: Object: It could be an array, list, vector, etc. Example 1: # R Program to illustrate # the use of is.character func
1 min read
Get Transpose of a Matrix or Data Frame in R Programming - t() Function
t() function in R Language is used to calculate transpose of a matrix or Data Frame. Syntax: t(x) Parameters: x: matrix or data frame Example 1: # R program to illustrate # t function # Getting R Biochemical Oxygen Demand Dataset BOD # Calling t() function t(BOD) Output: Time demand 1 1 8.3 2 2 10.3 3 3 19.0 4 4 16.0 5 5 15.6 6 7 19.8 [, 1] [, 2] [
1 min read
Compute the Hyperbolic arctangent of numeric data in R Programming - atanh() Function
atanh() function in R Language is used to compute the hyperbolic arctangent of numeric data. Syntax: atanh(x) Parameters: x: Numeric value, array or vector. Example 1: # R program to illustrate # atanh function # Calling atanh() function over # different numbers atanh(0) atanh(1) atanh(0.9) atanh(-0.8) Output: [1] 0 [1] Inf [1] 1.472219 [1] -1.0986
1 min read
Compute the Hyperbolic arcsine of numeric data in R Programming – asinh() Function
asinh() function in R Language is used to compute the hyperbolic arcsine of numeric data. Syntax:asinh(x) Parameters: x: Numeric value, array or vector Example 1: # R program to illustrate # asinh function # Calling asinh() function over # different numbers asinh(0) asinh(1) Output: [1] 0 [1] 0.8813736 Example 2: # R program to illustrate # asinh f
1 min read
Compute the Hyperbolic arccosine of numeric data in R Programming – acosh() Function
acosh() function in R Language is used to compute the hyperbolic arccosine of numeric data. Syntax:acosh(x) Parameters: x: Numeric value, array or vector Example 1: # R program to illustrate # acosh function # Calling acosh() function over # different numbers acosh(0) acosh(1) Output: [1] NaN [1] 0 Example 2: # R program to illustrate # acosh funct
1 min read
Getting class of different data types in R Programming - class() Function
class() function in R Language is used to return the class of data used as the arguments. Syntax: class(x) Parameters: x: specified data Example 1: # R program to illustrate # class function # Specifying "Biochemical oxygen demand" # data set x <- BOD x # Calling class() function class(x) Output: Time demand 1 1 8.3 2 2 10.3 3 3 19.0 4
1 min read
Getting type of different data types in R Programming - typeof() Function
typeof() function in R Language is used to return the types of data used as the arguments. Syntax: typeof(x) Parameters: x: specified data Example 1: # R program to illustrate # typeof function # Specifying "Biochemical oxygen demand" # data set x <- BOD x # Calling typeof() function typeof(x) Output: Time demand 1 1 8.3 2 2 10.3 3 3 1
1 min read
Merge Two Data Frames by common Columns in R Programming - merge() Function
merge() function in R Language is used to merge two data frames by common columns. Syntax: merge(arg1, arg2, by.x, by.y) Parameters: arg1 and arg2: Data frames to be merged by.x: Common argument of first data frame by.y: Common argument of second data frame Example 1: # R program to merge two data frames # Creating data frames df1 <- data.frame(
1 min read
Creating a Data Frame from Vectors in R Programming
A vector can be defined as the sequence of data with the same datatype. In R, a vector can be created using c() function. R vectors are used to hold multiple data values of the same datatype and are similar to arrays in C language. Data frame is a 2 dimensional table structure which is used to hold the values. In the data frame, each column contain
5 min read
Reordering of a Data Set in R Programming - arrange() Function
arrange() function in R Language is used for reordering of table rows with the help of column names as expression passed to the function. Syntax: arrange(x, expr) Parameters: x: data set to be reordered expr: logical expression with column name Example 1: # R program to reorder a data set # Loading library library(dplyr) # Calling dataset x <- B
1 min read
Check if an Object is of Complex Data Type in R Programming - is.complex() Function
is.complex() function in R Language is used to check if the object passed to it as argument is of complex data type. Syntax: is.complex(x) Parameters: x: Object to be checked Example 1: # R program to check if # object is of complex type # Calling is.complex() function is.complex(1 + 0i) is.complex(1.5 + 2i) is.complex(-1.5 + 2i) Output: [1] TRUE [
1 min read
Convert a Data Frame into a Molten Form in R Programming - melt() Function
function in R Language is used to combine multiple columns of s Data Frame into a single column. Syntax: melt(x, na.rm, value.name) Parameters: x: data to be melted na.rm: Boolean value to remove NA value.name: Setting column names Example 1: # R program to reshape data frame # Loading library library(reshape) # Creating a data frame a <- data.f
2 min read
Choose Specific Columns of a Data Frame in R Programming - select() Function
select() function in R Language is used to choose whether a column of the data frame is selected or not. Syntax: select(x, expr) Parameters: x: Data frame expr: condition for selection Example 1: # R program to select specific columns # Loading library library(dplyr) # Create a data frame d <- data.frame( name = c("Abhi", "Bhavesh
2 min read
Condense Column Values of a Data Frame in R Programming - summarise() Function
summarise() function in R Language is used to condense various values of column of a data frame to one value. Syntax: summarise(x, expr) Parameters: x: Data Frame expr: Operation to condense data Example 1: # R program to condense data # of a data frame # Loading library library(dplyr) # Create a data frame d <- data.frame( name = c("Abhi
1 min read
Take Random Samples from a Data Frame in R Programming - sample_n() Function
sample_n() function in R Language is used to take random sample specimens from a data frame. Syntax: sample_n(x, n) Parameters: x: Data Frame n: size/number of items to select Example 1: # R program to collect sample data # from a data frame # Loading library library(dplyr) # Create a data frame d <- data.frame( name = c("Abhi", "
1 min read
three90RightbarBannerImg