函数R中的错误,二元运算符的非数字参数

时间:2016-07-12 08:19:23

标签: r function arguments normalization min

我在R中创建了这个函数,它将创建规范化。在data2中,YearlyIncome列与最低值到最高值存在很大差异。我希望规范化转换值从0到1。

apply函数的值值覆盖为YearlyIncome

    > x <- data2$YearlyIncome
> a <- min(x)
> b <- max(x)
> fun <- function(x){  (x - a) /  (b - a) }
> fun(data$YearlyIncome)
Error in data$YearlyIncome : object of type 'closure' is not subsettable
> fun <- function(x){ (x - min(x))/(max(x) - min(x)) }
> fun(data2[1])
 Show Traceback

 Rerun with Debug
 Error in FUN(X[[i]], ...) : 
  only defined on a data frame with all numeric variables 
But I got this error:
>Error in x - a : non-numeric argument to binary operator

那我现在该怎么办?

1 个答案:

答案 0 :(得分:0)

在这里,我们是第一列的子集。不需要apply

fun(data$YearlyIncome)

其中

fun <- function(x){ (x - min(x))/(max(x) - min(x)) }