reshape2
is a package that consists mainly of two functions: melt
and dcast
/acast
. Generally, it could be said that melt()
transforms one row to multiple and shorter rows while dcast()
and acast()
do exactly the opposite.
The melt()
function, basically, transforms one row of data to many by pivoting a set of variables (the measure
variables) over a set of other variables (the id
variables). The function is called as follows:
melt(dataset,id.vars,measure.vars,variable_name)
The id
variables are usually factors or characters while the measure variables are the numeric ones. In fact, this is the behavior by default if none of the arguments are specified. variable_name
is the name that adopts the column where the variables are specified (variable
by default):
> library(reshape2) > data(iris) > melt(iris) Using Species as id variables Species variable value 1 setosa Sepal.Length 5.1 2 setosa Sepal.Length 4.9 3 setosa Sepal.Length 4.7 4 setosa Sepal.Length...