2

Suppose I assign data=Abortion (Abortion data set given in the ltm package). I have some function where one of the inputs is data.

While using the function, I will write.

function.name(data=Abortion)

For writing the summary of the results I want the name of the data set I used; here in this case it is Abortion.

How can I get that name back?

In more general sense. suppose I have some object which has some name abc. I assign xyz=abc and now how can I get the name abc back?

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • Maybe something like `dname <- deparse(substitute(data))` in your function. –  Jun 19 '14 at 09:26
  • Please clarify, why do you need this? Maybe you should rethink your approach... – zx8754 Jun 19 '14 at 09:28
  • @Pascal `dname <- deparse(substitute(data))` will just give back data name as string `"data"`, this is not what OP wants. – zx8754 Jun 19 '14 at 09:30
  • The solution by @Pascal should work for your specific case. There isn't a general solution for this problem since objects typically don't keep a history of the variable names they have been assigned to. – asb Jun 19 '14 at 09:30
  • @Pascal , suggestion by you doesn't work. –  Jun 19 '14 at 09:38
  • @zx8754, you are right it just gives "data" as an output. I need it in the summary of the result. If somebody is looking at my output they should know which data set I used. –  Jun 19 '14 at 09:38
  • @asb, for the time being, we can consider this data set as matrix only. –  Jun 19 '14 at 09:40
  • You need to post here a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and the desired output and what have you tried. Because I'm struggling to even understand what you want – David Arenburg Jun 19 '14 at 10:39

1 Answers1

0

I suggest to rethink your approach. I assume you are trying to loop through different datasets and get results. Try following example:

#dummy data
dat1 <- runif(10)
dat2 <- runif(10)
dat3 <- runif(10)

#my function
myfunc <- function(data) max(data)

#make a list - creating list of data manually, this is done automatically,e.g.:
# lapply(list.files(),read.table)
all_dat <- list(dat1,dat2,dat3)
#add names to list
names(all_dat) <- c("dat1","dat2","dat3")

#loop through dat1,2,3
sapply(all_dat,myfunc)
zx8754
  • 52,746
  • 12
  • 114
  • 209
  • let me put this question in this way. suppose dat1=runif(10) and data=dat1 needless to day but I have to keep changing the dat1 every time I change the data. myfunc = function(data){ code which uses data as input variable maytimes} for displaying summary of the result I am using "paste" command like paste("data used:" ,var) now this var should give me "dat1" somehow. –  Jun 19 '14 at 10:08
  • Use paste outside function? `res <- paste(deparse(substitute(dat1)),myfunc(data=dat1))` – zx8754 Jun 19 '14 at 10:16
  • this is making us write that data name ("dat1") more than once. Can't we do something like once we assign data=dat1 initially, before entering into the function. then we don't have to recall what was the name we used initially. –  Jun 19 '14 at 10:26
  • Variables do not keep record of which variables they were assigned from. You need to rethink why you need this... – zx8754 Jun 19 '14 at 10:29