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

Function Error in R, non-numeric argument to binary operator

我正在 R 中创建这个函数,它将创建规范化。在 data2 中,YearlyIncome 列从最低值到最高值差异很大。我想要规范化将值从 0 转换为 1。

应用函数的值覆盖为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

那我现在该怎么办?

在这里,我们对第一列进行子集化。不需要 apply

fun(data$YearlyIncome)

其中

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