Generally, in R, data processing is done by taking data as an input from 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 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 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 it’s output.

**Syntax:**

t(Matrix/ Data frame)

**Example:**

`# 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` |

*chevron_right*

*filter_none*

**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 usingfunction.`cbind()`

**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

function.**rbind()****Syntax:**rbind(x1, x2, x3)

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

**Example:**`# 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)`

*chevron_right**filter_none***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

function provided both the data frames should have the same column names. We may merge the two data frames based on a key value.**merge()****Syntax:**merge(dfA, dfB, ...)

**Example:**`# 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)`

*chevron_right**filter_none***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 method 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:**`# melt and cast`

`library(MASS)`

`library(reshape)`

`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"`

`))`

`print`

`(`

`"Melting"`

`)`

`m <`

`-`

`melt(a,`

`id`

`=`

`c(`

`"id"`

`,`

`"point"`

`))`

`print`

`(m)`

`print`

`(`

`"Casting"`

`)`

`idmn <`

`-`

`dcast(a,`

`id`

`~variable, mean)`

`print`

`(idmn)`

*chevron_right**filter_none***Output:**Melting id points variable value 1 1 x1 5 1 2 x1 3 2 1 x1 6 2 2 x1 2 1 1 x2 6 1 2 x2 5 2 1 x2 1 2 2 x2 4 Casting id x1 x2 1 4 5.5 2 4 2.5

## Recommended Posts:

- Reshaping a Data Frame in Julia
- Data Wrangling in R Programming - Data Transformation
- Convert an Object to Data Frame in R Programming - as.data.frame() Function
- Check if the Object is a Data Frame in R Programming - is.data.frame() Function
- Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function
- Modify Data of a Data Frame with an Expression in R Programming - with() Function
- Generate a set of Sample data from a Data set in R Programming - sample() Function
- Data Handling in R Programming
- Data Structures in R Programming
- R Programming for Data Science
- Reading Tabular Data from files in R Programming
- How To Import Data from a File in R Programming
- Accessing variables of a data frame in R Programming - attach() and detach() function
- Comparing values of data frames in R Programming - all_equal() Function
- Convert type of data object in R Programming - type.convert() Function
- Exporting Data from scripts in R Programming
- Create Matrix and Data Frame from Lists in R Programming
- Check if Object is of the Character Data type in R Programming - is.character() Function
- Get Transpose of a Matrix or Data Frame in R Programming - t() Function
- Create a Data Frame of all the Combinations of Vectors passed as Argument in R Programming - expand.grid() Function

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.