r - 在 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
在哪里添加新列,如果没有,是否有这样做的原因?
解决方案
查看 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>
似乎怀疑修改是否会受到欢迎,因为您已经有了一个可行的解决方案。
推荐阅读
- apache-spark - Glue Crawler 可以爬取 deltalake 文件以在 aws 胶水目录中创建表吗?
- c# - Azure Active Directory: Bearer error="invalid_token", error_description="The signature is invalid"
- scala - IntelliJ 在创建 Scala 项目后在 build.sbt 中给出“无法解析符号”的错误
- python - 如何使用 python matplotlib 调整水平图表上的线宽
- mysql - 外键约束不适用于 ALTER TABLE
- makefile - 使用 makefile/make 自动生成 .map
- javascript - 使用 jQuery 选择器获取 div 高度?
- javascript - 来自我的 API 的 Material UI CardMedia 图像数据
- javascript - Angular 与 RxJS - 数组的 Reduce 函数在订阅 Observable 时不起作用
- python - Cython 中的这个声明是什么?cdef PyObject **工人。它是指向指针的指针吗?