Is it possible to change an R data frame within a function without calling the function from a reference to that object?
For example, say I have a function character_func()
that I want to change data frames like mtcars2
so that mpg
becomes a character value.
I would like to just use: character_func(mtcars2)
EDIT: so without assigning the function to an object.
The deparse-substitute trick in this solution is not working.
Example:
mtcars2 <- mtcars
character_func <- function(df) {
df <- df %>%
mutate(mpg_c = as.character(mpg))
## WARNING occurs when next line runs
assign(deparse(substitute(df)), df)
}
character_func(mtcars2)
The assign() call returns the warning:
In assign(deparse(substitute(df)), df) :
only the first element is used as variable name