我正在尝试使我的数据框的所有列都处于相同的比例。
现在我有这样的东西... a在0-1范围内b在100范围内而c在1-5范围内
a b c
0 89 4
1 93 3
0 88 5
我将如何达到这样的100级...
a b c
0 89 80
100 93 60
0 88 100
我希望这有点清楚..我已经尝试过scale(),但似乎无法使其正常工作。
使用scale
,如果dat
是数据框的名称:
## 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)))
对于这样的简单情况,您也可以编写自己的函数。
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))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句