简体   繁体   English

对r中的数据集中的所有变量进行排名和最小-最大范数

[英]Rank and min-max norm all variables in a dataset in r

I am wanting to transform all but one of the variable in my dataset and include these new, transformed variables into a new dataset. 我想转换数据集中除一个变量之外的所有变量,并将这些新的,转换后的变量包括到一个新的数据集中。 I imagine this should not be that difficult, however I have been running into some issues. 我想这应该没那么难,但是我遇到了一些问题。 The transformation I am trying to do is a min-max norm of the rank. 我要进行的变换是等级的最小-最大范数。 I have been running the following code: 我一直在运行以下代码:

#create min-max norm function
normalize <- function(x) {
    return((x-min(x))/(max(x)-min(x)))
}

cols <- c(1:ncol(dt))
dt[cols] <- normalize(rank(dt[cols]))

However, when I run this code I receive the following error: 但是,当我运行此代码时,出现以下错误:

 Error: Can't use matrix or array for column indexing

I was wondering if someone could please help me out. 我想知道是否有人可以帮助我。 Thank you in advance. 先感谢您。

rank and normalize both take vectors. 对两个向量进行ranknormalize You can't give them data frames. 您不能给他们数据帧。

sapply and lapply are the classic utilties for apply ing function to columns of a data frame. sapplylapply申请 ING功能的数据帧的列的经典工具类。 A couple ways to do it: 几种方法可以做到这一点:

# one step with an anonymous function, calling both normalize and rank
dt[cols] = lapply(dt[cols], function(x) normalize(rank(x)))

However, your error seems to suggest that your cols object is a matrix or array. 但是,您的错误似乎表明您的cols对象是矩阵或数组。 So make sure that cols is what you think it is. 因此,请确保cols是您所认为的。 And the methods I mention above assume that dt is a data.frame (or data.table , or tibble ). 和我上面提到的方法假定dtdata.frame (或data.table ,或tibble )。 If dt is a matrix , array , or something else, they will not work as expected. 如果dtmatrixarray或其他东西,它们将无法按预期工作。 Look at str(dt) to check (and perhaps edit that info in to your question if you still have problems). 查看str(dt)进行检查(如果仍然有问题,也可以在问题中编辑该信息)。 # two steps, one at a time dt[cols] = lapply(dt[cols], rank) dt[cols] = lapply(dt[cols], normalize) #两步,一次dt [cols] = lapply(dt [cols],等级)dt [cols] = lapply(dt [cols],归一化)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM