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

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

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


当前回答

最简单的方法是使用包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"

其他回答

只有在因子标签与原始值匹配的情况下才有可能。我将用一个例子来解释。

假设数据是向量x:

x <- c(20, 10, 30, 20, 10, 40, 10, 40)

现在我将创建一个带有四个标签的因子:

f <- factor(x, levels = c(10, 20, 30, 40), labels = c("A", "B", "C", "D"))

1) x的类型是double, f的类型是integer。这是第一个不可避免的信息损失。因子总是存储为整数。

> typeof(x)
[1] "double"
> typeof(f)
[1] "integer"

2)如果只有f可用,则不可能恢复到原始值(10,20,30,40)。我们可以看到f只包含整数值1、2、3、4和两个属性——标签列表(“A”、“B”、“C”、“D”)和类属性“factor”。仅此而已。

> str(f)
 Factor w/ 4 levels "A","B","C","D": 2 1 3 2 1 4 1 4
> attributes(f)
$levels
[1] "A" "B" "C" "D"

$class
[1] "factor"

要恢复到原始值,我们必须知道在创建因子时使用的级别值。这里是c(10,20,30,40)如果我们知道原始的水平(以正确的顺序),我们可以恢复到原始的值。

> orig_levels <- c(10, 20, 30, 40)
> x1 <- orig_levels[f]
> all.equal(x, x1)
[1] TRUE

这只在为原始数据中的所有可能值定义了标签的情况下才有效。

所以如果你需要原始值,你必须保留它们。否则,很有可能无法仅从一个因素得到反馈。

注意:这个特殊的答案不是用于将数值因子转换为数字,而是用于将分类因子转换为相应的级别数字。


这篇文章中的每个答案都没有为我产生结果,NAs正在生成。

y2<-factor(c("A","B","C","D","A")); 
as.numeric(levels(y2))[y2] 
[1] NA NA NA NA NA Warning message: NAs introduced by coercion

对我有用的是——

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

如果因子级别是整数,则Strtoi()有效。

如果有数据帧,可以使用hablar::convert。语法很简单:

样本df

library(hablar)
library(dplyr)

df <- dplyr::tibble(a = as.factor(c("7", "3")),
                    b = as.factor(c("1.5", "6.3")))

解决方案

df %>% 
  convert(num(a, b))

给你:

# A tibble: 2 x 2
      a     b
  <dbl> <dbl>
1    7.  1.50
2    3.  6.30

或者如果你想让一列是整数,一列是数字:

df %>% 
  convert(int(a),
          num(b))

结果:

# A tibble: 2 x 2
      a     b
  <int> <dbl>
1     7  1.50
2     3  6.30

对于级别完全为数值的因子,Type.convert (f)是另一个基本选项。

性能方面,它相当于as.numeric(as.character(f)),但速度远不如as.numeric(levels(f))[f]。

identical(type.convert(f), as.numeric(levels(f))[f])

[1] TRUE

也就是说,如果vector在第一个实例中被创建为因子的原因没有被解决(即它可能包含一些不能被强制为数字的字符),那么这种方法将不起作用,它将返回一个因子。

levels(f)[1] <- "some character level"
identical(type.convert(f), as.numeric(levels(f))[f])

[1] FALSE