在 dplyr 中,使用 mutate 是否可以指定在哪里添加新列?

5

目前我必须使用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个回答

3

查看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>

现有的解决方案已经可用,因此对修改持怀疑态度。


2

关于这个问题,dplyr的github页面上有一个功能请求。您可以在这里阅读相关内容。但目前它仍然保持原样。

但是您可以随时将自己的原因添加到讨论中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接