Hi, I would like to add column to an existing data frame that contains the name of that data frame. The value in the new column would be dynamic, i.e. would depend on the name of the data frame.
For example, say I create the following data frame called "my_df":
This is a neat trick! What is substitute(x) doing? I tried reading the help documentation on it, but could not make sense of it! Hoping for a plain English understanding of this trick.
Hi @Piranha,
The trick takes advantages of the fact that substitute() returns the input without evaluating it when the env argument is .GlobalEnv (usually the default). So substitute(x) returns x, independent of what kind of object x is. Using as.character() this output is then transformed into a character so that it can be added to the data frame.
x<-c(1,2,3)
substitute(x)
#> x
eval(substitute(x))
#> [1] 1 2 3
as.character(substitute(x))
#> [1] "x"
However, I have no idea in which context this might be usefull, and I don't know what substitue() is used for except for some shady tricks to get the name of an object