R 如何在不丢失信息的情况下将因子转换为整数\数字?
当我将因子转换为数字或整数时,我得到的是底层的级别代码,而不是作为数字的值R 如何在不丢失信息的情况下将因子转换为整数\数字?,r,casting,r-faq,R,Casting,R Faq,当我将因子转换为数字或整数时,我得到的是底层的级别代码,而不是作为数字的值 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.
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
应用于
一个因素是没有意义的,可能
通过隐含的强迫发生。到
将系数f
转换为
近似于其原始数值
值,为。数值(级别(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)
值上执行到数值的转换,而不是在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
请参阅的警告部分: 特别是,
as.numeric
应用于
一个因素是没有意义的,可能
通过隐含的强迫发生。到
将系数f
转换为
近似于其原始数值
值,为。数值(级别(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)
值上执行到数值的转换,而不是在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
R有许多(未记录的)方便函数用于转换系数:
as.character.factor
as.data.frame.factor
as.Date.factor
as.list.factor
as.vector.factor
as.numeric.factor <- function(x) {as.numeric(levels(x))[x]}
as.numeric.factorR有许多(未记录的)方便函数用于转换因子:
as.character.factor
as.data.frame.factor
as.Date.factor
as.list.factor
as.vector.factor
但令人烦恼的是,没有什么可以处理因子->数值转换。作为Joshua Ulrich答案的延伸,我建议通过定义自己的惯用功能来克服这一遗漏:
as.numeric.factor <- function(x) {as.numeric(levels(x))[x]}
as.numeric.factor只有在因子标签与原始值匹配的情况下才可能。我会举例说明
假设数据是矢量x
:
x <- c(20, 10, 30, 20, 10, 40, 10, 40)
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
>原始级别x1全部相等(x,x1)
[1] 真的
只有在为原始数据中所有可能的值定义了标签的情况下,这才有效
因此,如果需要原始值,则必须保留它们。否则,很有可能无法仅从因子返回到它们。仅在因子标签与原始值匹配的情况下才有可能。我会举例说明
levels(f)[1] <- "some character level"
identical(type.convert(f), as.numeric(levels(f))[f])
[1] FALSE
假设数据是矢量x
:
x <- c(20, 10, 30, 20, 10, 40, 10, 40)
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
>原始级别x1全部相等(x,x1)
[1] 真的
只有在为原始数据中所有可能的值定义了标签的情况下,这才有效
因此,如果需要原始值,则必须保留它们。否则,很有可能无法仅从因子返回到它们。最简单的方法是使用包中的取消因子
函数,该函数可以接受因子向量甚至数据帧:
unfactor(your_factor_variable)
levels(f)[1] <- "some character level"
identical(type.convert(f), as.numeric(levels(f))[f])
[1] FALSE
这个例子可以是一个快速的开始:
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"
#加载包
库(“varhandle”)
#把虹膜传给unsector
商标
as.integer(y2)
# [1] 1 2 3 4 1
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
as.numeric(trimws(x_factor_var))
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)
}
)
factor2number <- function(x){
data.frame(levels(x), 1:length(levels(x)), row.names = 1)[x, 1]
}
factor2number(yourFactor)
identical(type.convert(f), as.numeric(levels(f))[f])
[1] TRUE
levels(f)[1] <- "some character level"
identical(type.convert(f), as.numeric(levels(f))[f])
[1] FALSE