In R, I want to rename all the columns that starts with some prefix (say "oldprefix1", "oldprefix2", "oldprefix3", ...
to "newprefix1", "newprefix2", "newprefix3", ...
) inside a function. The following code works:
change = function(df) {
select(df, newprefix = starts_with('oldprefix') )
}
change(test)
But, I would like to pass a string with the new prefix as parameter to the function:
change2 = function(df, prefix) {
dots = paste0(prefix," = starts_with('oldprefix')"
select_(df, dots)
}
change2(test, "newprefix")
I have tried using select_()
and .dots
, but I cannot get it to work together with the starts_with()
function. I get the error Error in eval(expr, envir, enclos) :
could not find function "starts_with"
.