当我将因子转换为数字或整数时,我得到的是底层的级别代码,而不是数字形式的值。
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
有没有更好的方法将因数转换为数字?
从我能读到的许多答案中,唯一给出的方法是根据因素的数量扩大变量的数量。如果你有一个级别为“dog”和“cat”的变量“pet”,你最终会得到pet_dog和pet_cat。
在我的例子中,我希望保持相同数量的变量,通过将因子变量转换为数值变量,以一种可以应用于许多级别的许多变量的方式,例如cat=1和dog=0。
对应的解决方案如下:
crime <- data.frame(city = c("SF", "SF", "NYC"),
year = c(1990, 2000, 1990),
crime = 1:3)
indx <- sapply(crime, is.factor)
crime[indx] <- lapply(crime[indx], function(x){
listOri <- unique(x)
listMod <- seq_along(listOri)
res <- factor(x, levels=listOri)
res <- as.numeric(res)
return(res)
}
)
注意:这个特殊的答案不是用于将数值因子转换为数字,而是用于将分类因子转换为相应的级别数字。
这篇文章中的每个答案都没有为我产生结果,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
最简单的方法是使用包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"
如果有数据帧,可以使用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