Using the reshape2 package, we can perform the recasting operation on the dataframe.

Recasting is a process of manipulating a dataframe in terms of its variable.

Recasting has two steps :

The types of variable used for recasting are :

Identifier Variable

Discrete variables like “Name”, “Month” etc.

Measurement Variables

Numeric variables like “Weight”, “Age” etc.

Note : Categorical and date variable are not measurement variables.

Let’s create a dataframe for our illustration :

Name = c("Senthil", "Senthil", "Sam", "Sam")
Month = c("Jan", "Feb", "Jan", "Feb")
BS = c(141.2, 139.3, 135.2,160.1)
BP = c(90, 78, 80, 81)

ed = data.frame(Name, Month, BS, BP)
print(ed)
##      Name Month    BS BP
## 1 Senthil   Jan 141.2 90
## 2 Senthil   Feb 139.3 78
## 3     Sam   Jan 135.2 80
## 4     Sam   Feb 160.1 81

reshape2 Installation For Recasting Operation

For recasting operation, we need a R-library named reshape2.

We have to write the following code for installing and calling the library into our console :

install.packages("reshape2") #For installing the package
library("reshape2") #For calling the package

The Melt Operation :

The following code perfroms the “melt” operation :

library("reshape2")

DF = melt( ed, 
           id.vars = c("Name","Month"), 
           measure.vars = c("BS","BP") )
print(DF)
##      Name Month variable value
## 1 Senthil   Jan       BS 141.2
## 2 Senthil   Feb       BS 139.3
## 3     Sam   Jan       BS 135.2
## 4     Sam   Feb       BS 160.1
## 5 Senthil   Jan       BP  90.0
## 6 Senthil   Feb       BP  78.0
## 7     Sam   Jan       BP  80.0
## 8     Sam   Feb       BP  81.0

The Cast Operation :

If we have two columns with categorical variables named as col1 And col2 then,

col1 ~ col2 Means we want to show col1 by col2.So, col1 will remain as it is but, each element of col2 has to become variables/columns to show the respective values of col1.

Let’s see an example of casting operation as follows :

library("reshape2")
DF2 = dcast(DF, 
            variable+Month ~ Name, 
            value.var = "value")
print(DF2)
##   variable Month   Sam Senthil
## 1       BS   Feb 160.1   139.3
## 2       BS   Jan 135.2   141.2
## 3       BP   Feb  81.0    78.0
## 4       BP   Jan  80.0    90.0

Recasting Operation In Single Step

We can perform both melting and casting operations on the dataframe ed in a single line as follows :

library("reshape2")
DF3 = recast(ed,
             variable+Month ~ Name, 
             id.var = c("Name","Month"))

print(DF3)
##   variable Month   Sam Senthil
## 1       BS   Feb 160.1   139.3
## 2       BS   Jan 135.2   141.2
## 3       BP   Feb  81.0    78.0
## 4       BP   Jan  80.0    90.0