【发布时间】:2020-03-09 14:52:34
【问题描述】:
我正在尝试将列表对象中的因子转换为字符。
我的第一次尝试是 purrr::map 与 mutate_if(is.factor,as.character) 的组合,但这不起作用。我用 Google 搜索了错误消息,但没有找到问题的答案。
我求助于使用base::lapply 来完成工作(确实如此)。
但是,我无法通过 purrr::map 方法发现错误。
有什么问题?
以下代码是我所面临情况的一个工作示例。
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(purrr)
a_matrix = matrix(data = sample(100,100, replace = T),nrow = 10)
a_df = data.frame(a_matrix)
cut_modif = function(x) {
cut(x,
breaks = quantile(x),
labels = c("A", "B", "C", "D"),
include.lowest = T,
right = T
)
}
# This one works
myResults = map(a_df,cut_modif)
myResults = lapply(myResults, as.character)
# This one DOES NOT work
myResults = map(a_df,cut_modif) %>%
map(mutate_if(is.factor, as.character))
#> Error in UseMethod("tbl_vars"): no applicable method for 'tbl_vars' applied to an object of class "function"
【问题讨论】: