Graphics Reference
In-Depth Information
hw
<-
heightweight
hw
<-
mutate(hw,
heightCm
=
heightIn
*
2.54
,
weightKg
=
weightLb
/
2.204
,
bmi
=
weightKg
/
(heightCm
/
100
)
^
2
)
See Also
See
Transforming Variables by Group
for how to perform group-wise transformations on data.
Transforming Variables by Group
Problem
You want to transform variables by performing operations on groups of data, as specified by a
grouping variable.
Solution
Use
ddply()
from the plyr package with the
transform()
function, and specify the operations:
library(MASS)
# For the data set
library(plyr)
cb
<-
ddply(cabbages,
"Cult"
, transform, DevWt
=
HeadWt
-
mean(HeadWt))
Cult Date HeadWt VitC DevWt
c39 d16
2.5
51 -0.40666667
c39 d16
2.2
55 -0.70666667
...
c52 d21
1.5
66 -0.78000000
c52 d21
1.6
72 -0.68000000
Discussion
Let's take a closer look at the
cabbages
data set. It has two grouping variables (factors):
Cult
,
which has levels
c39
and
c52
, and
Date
, which has levels
d16
,
d20
, and
d21
. It also has two
measured numeric variables,
HeadWt
and
VitC
:
cabbages
Cult Date HeadWt VitC
c39 d16
2.5
51
c39 d16
2.2
55
...