#Reshape2 Package
## while wide format is more readable, long format is easier to analyze. Therefore, it is useful to know how to convert between the two.
##There are two main functions in reshape2:
###melt - convert data from wide format to long format
###cast - convert data from long format to wide format
data("mtcars")
mtcars <-mtcars
library("reshape2")
## Warning: package 'reshape2' was built under R version 3.3.3
long_format <- melt(mtcars)
## No id variables; using all as measure variables
long_format1 <- melt(mtcars,id.vars=c('cyl','gear'),variable.name='carvariable',value.name='carvalue')
##There are two main types of the cast function:
###dcast - returns a dataframe as the output
###acast - returns a vector/matrix/array as the output
wide <- dcast(long_format1,cyl+gear ~ carvariable)#it give no of observations with respect to id variables
## Using carvalue as value column: use value.var to override.
## Aggregation function missing: defaulting to length