Here is another comparison between two basic functions transform
and within
, and a tidyverse function dplyr::mutate
. They all can be used for data munipulation, adding a new column to a data.frame.
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
One variable
Calculate horse power per cylinder (hp/cyl
) and add it to mtcars
tmp1 <- transform(mtcars, hpc = round(hp / cyl))
head(tmp1)
## mpg cyl disp hp drat wt qsec vs am gear carb hpc
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 18
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 18
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 23
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 18
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 22
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 18
tmp2 <- within(mtcars, {hpc <- round(hp / cyl)})
# tmp1 and tmp2 are identicial
head(tmp2)
## mpg cyl disp hp drat wt qsec vs am gear carb hpc
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 18
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 18
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 23
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 18
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 22
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 18
library(dplyr)
tmp3 <- mutate(mtcars, hpc = round(hp / cyl))
# tmp3 is ALMOST the same as tmp1 and tmp2, except that row names are missed
head(tmp3)
## mpg cyl disp hp drat wt qsec vs am gear carb hpc
## 1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 18
## 2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 18
## 3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 23
## 4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 18
## 5 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 22
## 6 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 18
two variables
A variable created by transform
process cannot be used1.
The example below fails.
tmp1 <- transform(mtcars, hpc = round(hp / cyl), hpc1 = hpc + 1)
While within
and dplyr::mutate
are OK2.
tmp2 <- within(mtcars, {hpc <- round(hp / cyl)
hpc1 <- hpc + 1 })
head(tmp2)
## mpg cyl disp hp drat wt qsec vs am gear carb hpc1
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 19
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 19
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 24
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 19
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 23
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 19
## hpc
## Mazda RX4 18
## Mazda RX4 Wag 18
## Datsun 710 23
## Hornet 4 Drive 18
## Hornet Sportabout 22
## Valiant 18
tmp3 <- mutate(mtcars, hpc = round(hp / cyl), hpc1 = hpc + 1)
head(tmp3)
## mpg cyl disp hp drat wt qsec vs am gear carb hpc hpc1
## 1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 18 19
## 2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 18 19
## 3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 23 24
## 4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 18 19
## 5 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 22 23
## 6 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 18 19
What is the difference between
transform
andwithin
Stack Overflow↩Data manipulation using transform(), within(), or plyr::mutate()↩