0

I have a data frame, df. I assigned it to an object, x.

x <- df

I want to create an object,y ,such that y takes the characters "df" , i.e. only the name of the data frame,df in double quotes, and not the data frame itself. However I want to assign "df" to y using the created object x as input.

The output, when y is called, should be:

> y
[1] "df"

I need to extract only the name of data frame, df, and assign it to object y.

I understand that one of the ways of doing this is by creating a list of all the dataframes in the environment and then calling 'df' from that list.

I want to know if there is any other way of doing this.

Thank You.

Jangy
  • 1
  • 2
  • 1
    Try using `deparse(quote(df))` – Tim Biegeleisen Aug 02 '18 at 13:07
  • 1
    Can we ask *why* you want to do this? It seems like an XY problem - whatever your larger goal is there is probably a better way to accomplish that goal in R than trying to do this very un-R-like task. – Gregor Thomas Aug 02 '18 at 13:08
  • @TimBiegeleisen but OP wants to only reference `x`, not `df` in creating the result :( – Gregor Thomas Aug 02 '18 at 13:09
  • @Gregor You must be seeing some requirement which I missed. +1 to your comment for using the term `un-R-like`. – Tim Biegeleisen Aug 02 '18 at 13:10
  • @Gregor I am creating a function that uses both df and "df", i.e. the data frame as well as the name of that data frame in quotes. This is why I need to extract the name of data.frame. – Jangy Aug 02 '18 at 13:15
  • @TimBiegeleisen from the question, *"However I want to assign "df" to y using the created object x as input."* – Gregor Thomas Aug 02 '18 at 13:17
  • Jangy, that's a good start, but why do you need a function to operate on the data and the name? Why do you need both? Just the object is enough for almost every single R function... – Gregor Thomas Aug 02 '18 at 13:37
  • @Gregor please take a look at this post. The second answer has worked for me here. That is why I need a function to operate on the data and the name. https://stackoverflow.com/questions/51635263/working-with-unequal-sized-dataframes-in-if-else-statement-in-r/51636946#51636946 – Jangy Aug 02 '18 at 13:42

1 Answers1

0

We need two assumptions:

  1. Both symbols are in same environment,

  2. you have not modified either of these and thus R has not made a deep copy.

Then you can look for symbols pointing to the same address in memory.

df <- data.frame(a = 1)
x <- df

library(pryr)
address(x)
#[1] "0x12d905c0"

adr <- vapply(ls(.GlobalEnv), function(x) {
  a <- as.symbol(x)
  setNames(eval(bquote(address(.(a))),.GlobalEnv), x)
  }, FUN.VALUE = character(1))
#          df            x 
#"0x12d905c0" "0x12d905c0"

names(adr[adr == address(x)])
[1] "df" "x"

However, if you pass df as a function parameter like foo(x = df) and need the name "df" inside the function, see this question: In R, how to get an object's name after it is sent to a function?

Roland
  • 127,288
  • 10
  • 191
  • 288
  • Thank You Sir. You are of great help... always. Much appreciated... Thanks a lot. @Roland – Jangy Aug 02 '18 at 14:03