How to do data reshape in R?
What is mean by data reshaping?
Data Reshaping in R is about changing the structure of the data into different formats. In R taking the input data as a data frame majority of the time.
It is very easy to extract data from one format into another format depends on the situation. R has many functions to split, merge, rbind, cbind and reshape in a data frame.
Join data frames
If you want to create multiple data frames into a single data frame can use cbind()function. Also, we can merge two data frames using rbind() function.
Data Reshape in R
Let us load the data into R. For the data frame, we can use the mass library.
library(mass) head(ships)
type year period service incidents 1 A 60 60 127 0 2 A 60 75 63 0 3 A 65 60 1095 3 4 A 65 75 1095 4 5 A 70 60 1512 6 6 A 70 75 3353 18
Goodness of Fit Test- Jarque-Bera Test in R »
Melt data frame
library(reshape) data<-melt(ships,id=c("type","year"))
type year variable value 1 A 60 period 60 2 A 60 period 75 3 A 65 period 60 4 A 65 period 75 5 A 70 period 60
How to Calculate Mahalanobis Distance in R »
Wide data frame
wide<-reshape(data,v.names="value",idvar=c("type","year"),timevar="variable",direction="wide") head(wide)
rename the column names
library(stringr) colnames(wide)<-str_replace_all(colnames(wide), pattern="value.",repl="")
type year period service incidents 1 A 60 60 127 0 9 B 60 60 44882 39 17 C 60 60 1179 1 25 D 60 60 251 0 33 E 60 60 45 0
Cast the Data Frame
We can cast the molten data into a new form where the aggregate of each type of ship for each year is created. It is done using the cast() function.
cast(data, type+year~variable,sum)
Sample Size Calculation Formula » Sampling Methods »
cbind / rbind data frame
If you want to combine two or more data frames use cbind. If the data frame has different dimensions use cbindPad function for the same.
data1<-ships[1:10,] data2<- ships[11:20,] cbind(data1,data2)
type year period service incidents type year period service incidents 1 A 60 60 127 0 B 65 60 28609 58 2 A 60 75 63 0 B 65 75 20370 53 3 A 65 60 1095 3 B 70 60 7064 12 4 A 65 75 1095 4 B 70 75 13099 44 5 A 70 60 1512 6 B 75 60 0 0
data1<-ships[1:10,] data2<- ships[11:20,] rbind(data1,data2)
merge data frame
If we want to club the data frames based on common ID. We can merge two data frames by using the merge() function. The data frames must have the same column names on which the merging happens.
merge(data1,data2,by="type")