Suppose I have the following data frame
library(tidyverse)
index <- 1:1000
df1 <- data.frame(index = glue::glue('index[{index}]'),
X = rnorm(1000))
I would like to write a function that takes a data frame, the name of a variable of that data frame, and returns a new data frame like the following
df2 <- df1 %>% mutate(index = as.numeric(gsub("index\\[(\\d+)\\]", "\\1", index)))
where, in this case the name of the variable is index.
I tried this:
myFunc <- function(data, name){
name <- enquo(name)
df1 <- data %>% mutate(!!name := as.numeric(gsub("index\\[(\\d+)\\]", "\\1", !!name)))
return(df1)
}
test <- myFunc(data = df1, name=index)
Alas, I get this error:
Error: The LHS of `:=` must be a string or a symbol
What am I doing wrong?
Thanks!