dplyr 0.7 等效于已弃用的 mutate_ [英] dplyr 0.7 equivalent for deprecated mutate_

查看:19
本文介绍了dplyr 0.7 等效于已弃用的 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)" )

将名称与其指令匹配并将所有内容转换为 quosures:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆