dplyr:mutate_at将字符列转换为因子列

时间:2018-02-20 19:28:57

标签: r dplyr

我正在尝试将字符列转换为我的data.table数据表对象中的因子列。我能做到:

df$a <- as.factor(df$a)

虽然这似乎有效,但也会出错:

Warning messages:
1: Unknown or uninitialised column: 'a'. 

上述问题似乎并不少见。在Fixing a multiple warning "unknown column"进行了探索并仍未解决。基于dplyr的解决方案似乎可以更改列类型。这就是我想要做的。让我们来看一个玩具示例。

假设我有data.table df

names(df)
[1] "a"  "b"  "c"                   
[4] "d"  "e"  "f"     

我试试:

df %>% mutate_at(.vars = vars(a), 
                 .funs = funs(factor))

但我明白了:

Error in overscope_eval_next(overscope, expr) : object 'a' not found

为什么找不到对象'a',我该如何解决?

另一个mutate_at解决方案的参考:dplyr change many data types

仅供参考,这是我的sessionInfo()

sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] bindrcpp_0.2        dplyr_0.7.4         bit64_0.9-7         bit_1.1-12          data.table_1.10.4-3
[6] h2o_3.16.0.2       

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.15     utf8_1.1.3       crayon_1.3.4     assertthat_0.2.0 bitops_1.0-6     R6_2.2.2        
 [7] jsonlite_1.5     magrittr_1.5     pillar_1.1.0     cli_1.0.0        rlang_0.1.6      tools_3.4.3     
[13] glue_1.2.0       RCurl_1.95-4.10  compiler_3.4.3   pkgconfig_2.0.1  bindr_0.1        tibble_1.4.2    

1 个答案:

答案 0 :(得分:2)

dplyr提供了双重分配运算符%<>%,该运算符从左到右进行管道传输,然后将管道的结尾重新分配回该运算符左侧的参数。

简而言之,这应该起作用: df$a %<>% as.factor