简体   繁体   中英

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?

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)) }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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