6.4 数据转换
transform 对数据框中的某些列做计算,取对数,将计算的结果单存一列加到数据框中
transform(iris[1:6, ], scale.sl = (max(Sepal.Length) - Sepal.Length) / (max(Sepal.Length) - min(Sepal.Length)))
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species scale.sl
## 1 5.1 3.5 1.4 0.2 setosa 0.375
## 2 4.9 3.0 1.4 0.2 setosa 0.625
## 3 4.7 3.2 1.3 0.2 setosa 0.875
## 4 4.6 3.1 1.5 0.2 setosa 1.000
## 5 5.0 3.6 1.4 0.2 setosa 0.500
## 6 5.4 3.9 1.7 0.4 setosa 0.000
验证一下 scale.sl
变量的第一个值
max(iris$Sepal.Length) - 5.1) / (max(iris$Sepal.Length) - min(iris$Sepal.Length)) (
## [1] 0.7777778
Warning: This is a convenience function intended for use interactively. For programming it is better to use the standard subsetting arithmetic functions, and in particular the non-standard evaluation of argument transform
can have unanticipated consequences.