how can i normalize the values of Sepal.Length by Species?
iris
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
...
# i have to divide by
tapply(iris$Sepal.Length, iris$Species, max)
setosa versicolor virginica
5.8 7.0 7.9
in other words i want to divide all values where Species=="setosa"
by 5.8 and so on finally i want to have a data frame with normalized values 0..1 in the Sepal.Length column.
Finally it should return
iris
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 0.8793103 3.5 1.4 0.2 setosa
...
Obviously there are a ton of ways to do this. I like the syntax of ave()
(see DWin's answer) or the data.table
package best:
library(data.table)
dt <- data.table(iris)
dt[, Sepal.Length:=(Sepal.Length)/max(Sepal.Length), by="Species"]
dt
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1: 0.8793103 3.5 1.4 0.2 setosa
# 2: 0.8448276 3.0 1.4 0.2 setosa
# 3: 0.8103448 3.2 1.3 0.2 setosa
# 4: 0.7931034 3.1 1.5 0.2 setosa
# 5: 0.8620690 3.6 1.4 0.2 setosa
# 146: 0.8481013 3.0 5.2 2.3 virginica
# 147: 0.7974684 2.5 5.0 1.9 virginica
# 149: 0.7848101 3.4 5.4 2.3 virginica
# 150: 0.7468354 3.0 5.1 1.8 virginica
df <- data.frame(dt) ## It's possible (but not necessary) to coerce back to
## a plain old data.frame
I'm strictly interpreting your desire to divide by the max values.
One option:
aggregate(iris$Sepal.Length,list(iris$Species),FUN = function(x) x/max(x))
and another, using ddply
from plyr (and scales all the columns at once:
ddply(iris,.(Species),colwise(function(x){x / max(x)}))
And a variant more like @Dwin's ave
example, that keep the other columns the same, but using ddply
:
ddply(iris,.(Species),transform,Sepal.Length = Sepal.Length / max(Sepal.Length))
iris$ratio_to_max <- ave( iris$Sepal.Length, list(iris$Species),
FUN= function(x) x/max(x))
#-------------
> str(iris)
'data.frame': 150 obs. of 6 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
$ ratio_to_max: num 0.879 0.845 0.81 0.793 0.862 ...
If you wanted to replace the Sepal.Length
column you could do so, but I generally avoid such destructive practice until I am really sure I got what I wanted. (And even then I feel guilty.) If you wanted this to be in separated list "packets" and throw away the original "Sepal.Length" column, you could use split
:
spl.iris <- split(iris[-1], iris$Species)
str(spl.iris)
I'm sure there's way better plyr or data table or even base ways:
L1 <- lapply(split(iris[, -5], iris$Species), function(x) apply(x, 2, scale))
L2 <- lapply(seq_along(L1), function(i) {
data.frame(SPecies=names(L1)[i], L1[[i]])
})
do.call(rbind, L2)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.