[R] Returning name of dataframe?

arun smartpink111 at yahoo.com
Thu Jun 20 03:39:26 CEST 2013


Hi,
May be this helps:

xyz<-function(x,y){
  z<-x+y
  print(paste("the sum of", deparse(substitute(x)), "and", deparse(substitute(y)),"is", sep=" "))
  z
}

xyz(a,b)
[1] "the sum of a and b is"
             [,1]        [,2]        [,3]        [,4]        [,5]
 [1,] -1.38805146 -1.15706888 -1.09420046  1.64717323  0.27514289
 [2,]  0.22261237 -0.73175081  1.68966845  0.22596732 -1.17044669
 [3,]  0.22500501 -0.75974026  0.90409054 -1.45650109  1.67901527
 [4,] -1.48843138 -1.94294190 -2.16271187  0.63399385 -1.57638747
 [5,] -1.82516894 -3.99381921  0.60723679  1.29199176  2.48361508
 [6,]  1.17253015  1.06681277 -0.92285478  0.17471185 -2.15750294
 [7,] -0.07154138 -0.66920922  1.05733601 -0.66156409 -1.40881929
 [8,]  0.10070360 -0.92133842 -1.13318982 -0.55892454  1.03856347
 [9,] -0.28225914 -1.24651183  2.64215721 -2.95011897 -0.88959061
[10,] -0.71393462 -1.80015777 -0.07966670  0.86487927 -0.02940905
[11,] -0.22453213 -0.78472943 -0.41515830  0.09789192 -0.47531826
[12,] -1.30255845 -0.40284910 -1.60752283  1.47922017  1.93232550
[13,]  1.18939368 -1.15079413  0.93800266  1.98916709 -0.10855782
[14,] -2.94616816 -1.95470339  0.51466407 -1.54365972  0.21532896
[15,] -1.18251544  0.60247030 -0.27653274 -1.29190483  2.16970664
[16,] -0.25704995  0.03947102 -0.07675004 -0.19652487 -0.61109230
[17,] -1.67056007 -0.43978334  0.46164805 -0.97022452 -0.92957787
[18,]  0.34908174  1.29140274 -0.69428669 -0.94739520  0.67424984
[19,] -0.07291139  0.14937308  0.25048958 -0.21737017  0.33106716
[20,]  0.45590314  1.78337639 -1.17198752 -0.59142492  0.70282106
A.K.


----- Original Message -----
From: Thomas Parr <thomas.parr at maine.edu>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, June 19, 2013 8:16 PM
Subject: [R] Returning name of dataframe?

I am writing a function and I would like to return the name of a data frame
in a paste call, but I can't figure out how to just get the name. The names
of the data frames used, won't be the same each time.  I have to be
overlooking the obvious.

#For example:

a<-replicate(5, rnorm(20))
b<-replicate(5, rnorm(20))

xyz<-function(x,y){
  z<-x+y
  print(paste("the sum of", x, "and", y,"is", sep=" "))
  z 
}

xyz(a,b)

#That function should return:

>"the sum of a and b is:"
>.and some data.

Not surprisingly, it returns all the data in the data frame.

Thanks,
Thomas

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list