【问题标题】:Error Standardising variables in R : 'only defined on a data frame with all numeric variables'R中的错误标准化变量:'仅在具有所有数字变量的数据框上定义'
【发布时间】:2017-03-21 16:12:35
【问题描述】:

我只是想将我的数据框变量集标准化为 100 分制。原始变量采用 10 分制,小数点后 4 位。

我可以看到我的错误并非闻所未闻,例如

Why am I getting a function error in seemingly similar R code?

Error: only defined on a data frame with all numeric variables with ddply on large dataset

但我已经验证所有变量都是数字使用

library(foreign)
library(scales)
ches <- read.csv("chesshort15.csv", header = TRUE)
ches2 <- ches[1:244, 3:10]
rescale(ches2, to = c(0,100), from = range(ches2, na.rm = TRUE, finite = TRUE))

这给出了错误: FUN(X[[i]], ...) 中的错误: 仅在具有所有数值变量的数据框上定义

我已经使用str(ches2) 验证了所有变量都是数字类型 - 见下文:

'data.frame':   244 obs. of  8 variables:
 $ galtan            : num  8.8 9 9.65 8.62 8 ...
 $ civlib_laworder   : num  8.5 8.6 9.56 8.79 8.56 ...
 $ sociallifestyle   : num  8.89 7.2 9.65 9.21 8.25 ...
 $ immigrate_policy  : num  9.89 9.6 9.38 9.43 9.13 ...
 $ multiculturalism  : num  9.9 9.6 9.57 8.77 9.07 ...
 $ ethnic_minorities : num  8.8 9.6 9.87 9 8.93 ...
 $ nationalism       : num  9.4 10 9.82 9 8.81 ...
 $ antielite_salience: num  8 9 9.47 8.88 8.38 

简而言之,我很困惑为什么它拒绝执行代码。

有关信息,Head(bb) 提供:

  galtan civlib_laworder sociallifestyle immigrate_policy multiculturalism ethnic_minorities
1  8.800           8.500           8.889            9.889            9.900             8.800
2  9.000           8.600           7.200            9.600            9.600             9.600
3  9.647           9.563           9.647            9.375            9.571             9.867
4  8.625           8.786           9.214            9.429            8.769             9.000
5  8.000           8.563           8.250            9.133            9.071             8.929
6  7.455           8.357           7.923            8.800            7.800             8.455
  nationalism antielite_salience
1       9.400              8.000
2      10.000              9.000
3       9.824              9.471
4       9.000              8.882
5       8.813              8.375
6       8.000              8.824

【问题讨论】:

    标签: r


    【解决方案1】:

    rescale 函数抛出该错误,因为它需要一个数字向量,而您正在向它提供数据帧。你需要迭代;遍历数据框上的每一列并单独缩放它们。

    试试这个:

    sapply(ches2, rescale, to = c(0,100))
    

    您不需要代码的 range(ches2, na.rm = TRUE, finite = TRUE) 部分,因为 rescale 足够聪明,可以自行删除 NA 值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-18
      • 1970-01-01
      • 2018-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多