dplyr将字符强制转换为因子

时间:2019-07-04 02:30:46

标签: r dplyr

我敢肯定,目前还没有找到一个很好的理由,但是...为什么dplyr会强制字符转换为因素,即使您明确地强制转换为字符呢?

> letters
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
> typeof(letters)
[1] "character"
> data.frame(
+   colA = as.character(letters), 
+   colB = as.character(LETTERS)
+ ) %>%
+   glimpse
Observations: 26
Variables: 2
$ colA <fct> a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
$ colB <fct> A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z

1 个答案:

答案 0 :(得分:5)

不是将ServiceImpl强制转换为ServiceImpl.kt,而是dplyrfactor构造函数),其中默认选项为data.frame。指定base R将解决问题

stringsAsFactors = TRUE

注意:不需要包装stringsAsFactors = FALSE


在使用data.frame( colA = letters, colB = LETTERS, stringsAsFactors = FALSE ) 时,选项为as.character,其默认设置为tidyverse

tibble