【发布时间】:2016-11-14 09:00:28
【问题描述】:
我正在 R 中创建这个函数,它将创建规范化。在data2、YearlyIncome 列中,从最小值到最大值的差异很大。我希望标准化将值从 0 转换为 1。
apply函数的value值覆盖到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
那我现在该怎么办?
【问题讨论】:
-
我刚刚通过电子邮件回答了这个问题,它没有解决您的问题吗?向我们展示完整的
data2数据框。 -
我会使用
fun(data2[,1]),因为data2[1]仍然是一个data.frame。另外,请检查str(data2)以查看第一列是否为数字变量。我用一个示例数据进行了测试,它对我有用。 -
@akun 所以 akun 你能改变你的答案,它将取代我以前的错误行吗?就像我在定义我的函数 fun
-
@akun 还有 akun 有没有办法在 R studio 中查看 csv 文件?
-
第一个错误来自拼写错误(
data而不是data2),第二个错误可能来自data2的第一列不是数字,但我们没有没有足够的信息来解决这个问题
标签: r function arguments normalization min