Graphics Reference
In-Depth Information
m
13.92
167
62.0
107.5 157.480
m
12.58
151
59.3
87.0 150.622
Discussion
For slightly easier-to-read code, you can use
transform()
or
mutate()
from the plyr package.
You only need to specify the data frame once, as the first argument to the function, meaning
these provide a cleaner syntax, especially if you are transforming multiple variables:
hw
<-
transform(hw, heightCm
=
heightIn
*
2.54
, weightKg
=
weightLb
/
2.204
)
library(plyr)
hw
<-
mutate(hw, heightCm
=
heightIn
*
2.54
, weightKg
=
weightLb
/
2.204
)
hw
sex ageYear ageMonth heightIn weightLb heightCm weightKg
f
11.92
143
56.3
85.0 143.002 38.56624
f
12.92
155
62.3
105.0 158.242 47.64065
...
m
13.92
167
62.0
107.5 157.480 48.77495
m
12.58
151
59.3
87.0 150.622 39.47368
It is also possible to calculate a new variable based on multiple variables:
# These all have the same effect:
hw
<-
transform(hw, bmi
=
weightKg
/
(heightCm
/
100
)
^
2
)
hw
<-
mutate(hw, bmi
=
weightKg
/
(heightCm
/
100
)
^
2
)
hw$bmi
<-
hw$weightKg
/
(hw$heightCm
/
100
)
^
2
hw
sex ageYear ageMonth heightIn weightLb heightCm weightKg bmi
f
11.92
143
56.3
85.0 143.002 38.56624 18.85919
f
12.92
155
62.3
105.0 158.242 47.64065 19.02542
...
m
13.92
167
62.0
107.5 157.480 48.77495 19.66736
m
12.58
151
59.3
87.0 150.622 39.47368 17.39926
The main functional difference between
transform()
and
mutate()
is that
transform()
cal-
culates the new columns simultaneously, while
mutate()
calculates the new columns sequen-
tially, allowing you to base one new column on another new column. Since
bmi
is calculated
from
heightCm
and
weightKg
, it is not possible to calculate all of them in a single call to
trans-
form()
;
heightCm
and
weightKg
must be calculated first, and then
bmi
, as shown here.
With
mutate()
, however, we can calculate them all in one go. The following code has the same
effect as the previous separate blocks: