在dplyr中,是否可以使用mutate指定添加新列的位置?

时间:2018-04-24 22:18:58

标签: r dataframe dplyr mutate

目前,我必须使用add_column直接将新列插入所需位置,或使用mutate,然后select使用新的所需列顺序。

mips.group <- str_extract(mips.manifest$PlateName, "[:alnum:]+_([[:alnum:]&&[^P]]+(_CL)?)?|(KORgex)")

mips.manifest %<>%
  add_column(MIPSGroup=mips.group, .after="PlateName")

是否可以直接告诉mutate添加新列的位置,如果没有,是否有原因?

2 个答案:

答案 0 :(得分:2)

看看mutate的代码,看起来它很容易,因为它最终会潜入C函数:

> mutate
function (.data, ...) 
{
    UseMethod("mutate")
}
<environment: namespace:dplyr>
> methods(mutate)
[1] mutate.data.frame* mutate.default*    mutate.tbl_df*    
see '?methods' for accessing help and source code
> getAnywhere(mutate.tbl_df)
A single object matching ‘mutate.tbl_df’ was found
It was found in the following places
  registered S3 method for mutate from namespace dplyr
  namespace:dplyr
with value

function (.data, ...) 
{
    dots <- named_quos(...)
    mutate_impl(.data, dots)
}
<environment: namespace:dplyr>
> mutate_impl
Error: object 'mutate_impl' not found
> getAnywhere(mutate_impl)
A single object matching ‘mutate_impl’ was found
It was found in the following places
  namespace:dplyr
with value

function (df, dots) 
{
    .Call(`_dplyr_mutate_impl`, df, dots)
}
<environment: namespace:dplyr>

似乎怀疑修改会受到欢迎,因为你已经有了可行的解决方案。

答案 1 :(得分:1)

dplyr的github页面上有关于此问题的功能请求。你可以阅读这个here。但现在它仍然保留原样。

但您可以随时将您的理由添加到讨论中。