Graphics Reference
In-Depth Information
Solution
Use melt() from the reshape2 package. In the anthoming data set, for each angle, there are
two measurements: one column contains measurements in the experimental condition and the
other contains measurements in the control condition:
library(gcookbook) # For the data set
anthoming
angle expt ctrl
-20 1 0
-10 7 3
0 2 3
10 0 3
20 0 1
We can reshape the data so that all the measurements are in one column. This will put the values
from expt and ctrl into one column, and put the names into a different column:
library(reshape2)
melt(anthoming, id.vars = "angle" , variable.name = "condition" , value.name = "count" )
angle condition count
-20
expt
1
-10
expt
7
0
expt
2
10
expt
0
20
expt
0
-20
ctrl
0
-10
ctrl
3
0
ctrl
3
10
ctrl
3
20
ctrl
1
This data frame represents the same information as the original one, but it is structured in a way
that is more conducive to some analyses.
Discussion
In the source data, there are IDvariables and measurevariables. The ID variables are those that
specify which values go together. In the source data, the first row holds measurements for when
angle is -20. In the output data frame, the two measurements, for expt and ctrl , are no longer
in the same row, but we can still tell that they belong together because they have the same value
of angle .
Search WWH ::




Custom Search