当我将因子转换为数字或整数时,我得到的是底层的级别代码,而不是数字形式的值。

f <- factor(sample(runif(5), 20, replace = TRUE))
##  [1] 0.0248644019011408 0.0248644019011408 0.179684827337041 
##  [4] 0.0284090070053935 0.363644931698218  0.363644931698218 
##  [7] 0.179684827337041  0.249704354675487  0.249704354675487 
## [10] 0.0248644019011408 0.249704354675487  0.0284090070053935
## [13] 0.179684827337041  0.0248644019011408 0.179684827337041 
## [16] 0.363644931698218  0.249704354675487  0.363644931698218 
## [19] 0.179684827337041  0.0284090070053935
## 5 Levels: 0.0248644019011408 0.0284090070053935 ... 0.363644931698218

as.numeric(f)
##  [1] 1 1 3 2 5 5 3 4 4 1 4 2 3 1 3 5 4 5 3 2

as.integer(f)
##  [1] 1 1 3 2 5 5 3 4 4 1 4 2 3 1 3 5 4 5 3 2

我不得不求助于粘贴来获得实际值:

as.numeric(paste(f))
##  [1] 0.02486440 0.02486440 0.17968483 0.02840901 0.36364493 0.36364493
##  [7] 0.17968483 0.24970435 0.24970435 0.02486440 0.24970435 0.02840901
## [13] 0.17968483 0.02486440 0.17968483 0.36364493 0.24970435 0.36364493
## [19] 0.17968483 0.02840901

有没有更好的方法将因数转换为数字?


当前回答

R有许多(未记录的)便利函数用于转换因子:

as.character.factor as.data.frame.factor as.Date.factor as.list.factor as.vector.factor ...

但令人烦恼的是,没有任何东西可以处理因子->数字转换。作为Joshua Ulrich回答的延伸,我建议通过定义你自己的惯用函数来克服这个遗漏:

as.double.factor <- function(x) {as.numeric(levels(x))[x]}

你可以把它存储在你的脚本开头,或者更好的存储在你的。rprofile文件中。

其他回答

最简单的方法是使用包varhandle中的unfactor函数,它可以接受一个因子向量,甚至一个数据帧:

unfactor(your_factor_variable)

下面这个例子可以作为一个快速的开始:

x <- rep(c("a", "b", "c"), 20)
y <- rep(c(1, 1, 0), 20)

class(x)  # -> "character"
class(y)  # -> "numeric"

x <- factor(x)
y <- factor(y)

class(x)  # -> "factor"
class(y)  # -> "factor"

library(varhandle)
x <- unfactor(x)
y <- unfactor(y)

class(x)  # -> "character"
class(y)  # -> "numeric"

你也可以在数据框架上使用它。例如虹膜数据集:

sapply(iris, class)

萼片。花萼长度。宽度花瓣。花瓣长度。宽度的物种 "数字" "数字" "数字" "因素"

# load the package
library("varhandle")
# pass the iris to unfactor
tmp_iris <- unfactor(iris)
# check the classes of the columns
sapply(tmp_iris, class)

萼片。花萼长度。宽度花瓣。花瓣长度。宽度的物种 "数字" "数字" "数字" "字符"

# check if the last column is correctly converted
tmp_iris$Species

[1] "setosa" "setosa" "setosa" "setosa" "setosa" [6] "setosa" "setosa" "setosa" "setosa" "setosa" [11] "setosa" "setosa" "setosa" "setosa" "setosa" [16] "setosa" "setosa" "setosa" "setosa" "setosa" [21] "setosa" "setosa" "setosa" "setosa" "setosa" [26] "setosa" "setosa" "setosa" "setosa" "setosa" [31] "setosa" "setosa" "setosa" "setosa" "setosa" [36] "setosa" "setosa" "setosa" "setosa" "setosa" [41] "setosa" "setosa" "setosa" "setosa" "setosa" [46] "setosa" "setosa" "setosa" "setosa" "setosa" [51] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" [56] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" [61] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" [66] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" [71] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" [76] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" [81] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" [86] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" [91] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" [96] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor" [101] "virginica" "virginica" "virginica" "virginica" "virginica" [106] "virginica" "virginica" "virginica" "virginica" "virginica" [111] "virginica" "virginica" "virginica" "virginica" "virginica" [116] "virginica" "virginica" "virginica" "virginica" "virginica" [121] "virginica" "virginica" "virginica" "virginica" "virginica" [126] "virginica" "virginica" "virginica" "virginica" "virginica" [131] "virginica" "virginica" "virginica" "virginica" "virginica" [136] "virginica" "virginica" "virginica" "virginica" "virginica" [141] "virginica" "virginica" "virginica" "virginica" "virginica" [146] "virginica" "virginica" "virginica" "virginica" "virginica"

参见?factor的警告部分:

特别地,作为。数值应用于 一个因素是没有意义的,而且可能 通过隐性胁迫发生。来 将因子f变换为 近似于它原来的数值 值,如.numeric(levels(f))[f]是 推荐,稍微多一点 效率比 as.numeric (as.character (f))。

R的常见问题解答也有类似的建议。


为什么as.numeric(levels(f))[f]比as.numeric(as.character(f))更有效?

As.numeric (as.character(f))实际上是As.numeric (levels(f)[f]),因此您是在长度(x)值上执行到numeric的转换,而不是在nlevels(x)值上执行转换。速度的差异将是最明显的长向量与很少的水平。如果这些值都是唯一的,那么在速度上就不会有太大的差异。无论您如何进行转换,该操作都不太可能成为代码中的瓶颈,因此不必过于担心。


一些时间

library(microbenchmark)
microbenchmark(
  as.numeric(levels(f))[f],
  as.numeric(levels(f)[f]),
  as.numeric(as.character(f)),
  paste0(x),
  paste(x),
  times = 1e5
)
## Unit: microseconds
##                         expr   min    lq      mean median     uq      max neval
##     as.numeric(levels(f))[f] 3.982 5.120  6.088624  5.405  5.974 1981.418 1e+05
##     as.numeric(levels(f)[f]) 5.973 7.111  8.352032  7.396  8.250 4256.380 1e+05
##  as.numeric(as.character(f)) 6.827 8.249  9.628264  8.534  9.671 1983.694 1e+05
##                    paste0(x) 7.964 9.387 11.026351  9.956 10.810 2911.257 1e+05
##                     paste(x) 7.965 9.387 11.127308  9.956 11.093 2419.458 1e+05

在游戏后期,偶然地,我发现trimws()可以将因子(3:5)转换为c(“3”,“4”,“5”)。然后可以调用as.numeric()。那就是:

as.numeric(trimws(x_factor_var))

如果你有很多因子列要转换成数字,

df <- rapply(df, function(x) as.numeric(levels(x))[x], "factor", how =  "replace")

这个解决方案对于包含混合类型的data.frames是健壮的,前提是所有的因子级别都是数字。

R有许多(未记录的)便利函数用于转换因子:

as.character.factor as.data.frame.factor as.Date.factor as.list.factor as.vector.factor ...

但令人烦恼的是,没有任何东西可以处理因子->数字转换。作为Joshua Ulrich回答的延伸,我建议通过定义你自己的惯用函数来克服这个遗漏:

as.double.factor <- function(x) {as.numeric(levels(x))[x]}

你可以把它存储在你的脚本开头,或者更好的存储在你的。rprofile文件中。