Search code examples
rfunctionargumentsnormalizationmin

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


I'm creating this function in R, that will create normalization. In data2, YearlyIncome column has large difference from lowest value to highest value. I want normalization convert value from 0 to 1.

The value values of apply function overwrite to 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

So what do I do now?


Solution

  • Here, we are subsetting the first column. The apply is not needed for that.

    fun(data$YearlyIncome)
    

    where

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