dplyr 0.7等效于不推荐使用的mutate_ [英] dplyr 0.7 equivalent for deprecated mutate_
问题描述
我在dplyr 0.7
中找不到替换将要弃用的 mutate _ 函数的方法.
I cannot find in dplyr 0.7
a way to replace the mutate_ function which is going to be deprecated.
在我的用例中, mutate _ 函数很有用:我在数据库(字符串格式)中存储了许多指令(可以根据需要进行过滤),并将这些指令应用于一个或几个数据帧.
The mutate_ function is useful in my use case : I store in a database (string format) many instructions (that can be filtered if needed) and apply these instructions to one or several data frames.
例如:
dplyr::tibble(test = "test@test") %>%
dplyr::mutate_(.dots = list("test2" = "substr(test, 1, 5)",
"test3" = "substr(test, 5, 5)"))
使用dplyr 0.7可以将变量和指令保留为字符吗?
Is there a way to do this with dplyr 0.7 keeping variables and instructions as character?
推荐答案
在 MrFlick 的基础上进行扩展例如,假设您有许多以字符串形式存储的指令,以及要分配给结果计算的相应名称:
To expand a little bit on MrFlick's example, let's assume you have a number of instructions stored as strings, as well as the corresponding names that you want to assign to the resulting computations:
ln <- list( "test2", "test3" )
lf <- list( "substr(test, 1, 5)", "substr(test, 5, 5)" )
将名称与他们的说明相匹配,然后将所有内容转换为quasures:
Match up names to their instructions and convert everything to quosures:
ll <- setNames( lf, ln ) %>% lapply( rlang::parse_quosure )
根据 aosmith 的建议,现在可以使用特殊的!!!
将整个列表传递给mutate运算符:
As per aosmith's suggestion, the entire list can now be passed to mutate, using the special !!!
operator:
tibble( test = "test@test" ) %>% mutate( !!! ll )
# # A tibble: 1 x 3
# test test2 test3
# <chr> <chr> <chr>
# 1 test@test test@ @
这篇关于dplyr 0.7等效于不推荐使用的mutate_的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!