Is there a way to normalize many rows of a data frame according to the maximum value per row, preferably in dplyr?
In iris
I want to find the maximum per row of the columns Sepal.length, Sepal.width, Petal.length and Petal.width. I then want to divide every value by this maximum to get a range from 0 to 1. I want to be able to give a range of columns across which to find the maximum, and preferably not have to call them by name.
Output goal:
> iris
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
> iris.normalized
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 3.5/5.1 1.4/5.1 0.2/5.1 setosa
I tried using the max
function to no avail. I'm glad for any help.
使用base R
,用pmax
计算每行的最大值,然后除以感兴趣的列以进行缩放
iris[1:4] <- iris[1:4]/do.call(pmax, iris[1:4])
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.