相关疑难解决方法(0)

使用`starts_with()`重命名列,其中new prefix是一个字符串

在R,我要重命名所有以一些前缀开始的列(说"oldprefix1", "oldprefix2", "oldprefix3", ..."newprefix1", "newprefix2", "newprefix3", ...)函数内部.以下代码有效:

change = function(df) {
    select(df, newprefix = starts_with('oldprefix') )
}
change(test)
Run Code Online (Sandbox Code Playgroud)

但是,我想将带有新前缀的字符串作为参数传递给函数:

change2 = function(df, prefix) {
    dots = paste0(prefix," = starts_with('oldprefix')"
    select_(df, dots)
}
change2(test, "newprefix")
Run Code Online (Sandbox Code Playgroud)

我尝试过使用select_().dots,但我不能让它与该starts_with()功能一起使用.我收到了错误Error in eval(expr, envir, enclos) : could not find function "starts_with".

r rename dplyr

6
推荐指数
1
解决办法
1381
查看次数

标签 统计

dplyr ×1

r ×1

rename ×1