2

I've written this function which just replaced each value in a data table's column with its substring:

substrColName <- function(df, colName, start) { df %>% mutate(colName = substr(colName, start=start, stop=nchar(colName))) } 

But every time I try running it I get the error:

Error in as.character(x) : cannot coerce type 'closure' to vector of type 'character' 

Now I've done a lot of research on why it won't work but I can't figure it out. I've read on some stuff with standard evaluations and lazyeval but nothing I try seems to work. Any help?

Thanks

2
  • Can you provide the data with which you ran the function on? Commented Oct 7, 2017 at 2:57
  • See this cran.r-project.org/web/packages/dplyr/vignettes/… to learn programming with dplyr. Commented Oct 7, 2017 at 3:02

1 Answer 1

2

Thanks, @ycw, that was a nice read. Got it working now after working through the article. This was the solution at the end of the day:

substrColName <- function(df, colName, start) { colNameQuo <- enquo(colName) df %>% mutate(!!quo_name(colNameQuo) := substr(!!colNameQuo, start=start,stop=nchar(!!colNameQuo))) } 

And this is ycw's comment.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.