如何将数据框中的每一列重新缩放到 0-100 的比例?(在 r 中) [英] How can i rescale every column in my data frame to a 0-100 scale? (in r)
本文介绍了如何将数据框中的每一列重新缩放到 0-100 的比例?(在 r 中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使我的数据框的所有列都处于相同的比例..
i am trying to get all the colums of my data frame to be in the same scale..
现在我有这样的东西......其中 a 在 0-1 范围内 b 在 100 范围内,c 在 1-5 范围内
right now i have something like this... where a is on a 0-1 scale b is on a 100 scale and c is on a 1-5 scale
a b c
0 89 4
1 93 3
0 88 5
我怎样才能达到这样的 100 级...
How would i get it to a 100scale like this...
a b c
0 89 80
100 93 60
0 88 100
我希望这有点清楚..我试过 scale() 但似乎无法让它工作.
i hope that is somewhat clear.. i have tried scale() but can not seem to get it to work.
推荐答案
使用 scale
,如果 dat
是您的数据框的名称:
Using scale
, if dat
is the name of your data frame:
## for one column
dat$a <- scale(dat$a, center = FALSE, scale = max(dat$a, na.rm = TRUE)/100)
## for every column of your data frame
dat <- data.frame(lapply(dat, function(x) scale(x, center = FALSE, scale = max(x, na.rm = TRUE)/100)))
对于这样的简单案例,您也可以编写自己的函数.
For a simple case like this, you could also write your own function.
fn <- function(x) x * 100/max(x, na.rm = TRUE)
fn(c(0,1,0))
# [1] 0 100 0
## to one column
dat$a <- fn(dat$a)
## to all columns of your data frame
dat <- data.frame(lapply(dat, fn))
这篇关于如何将数据框中的每一列重新缩放到 0-100 的比例?(在 r 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文