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
'a' and 'b' are vectors of length 100, so the sum of them would also produce a vector of 100. So in one case, your code should look like this:> 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", z, sep=" ")) + }> > xyz(a,b)[1] "the sum of 0.078437532748926 and 1.42398359867007 is 1.502421131419" [2] "the sum of -0.779261070429696 and 0.484730511200338 is -0.294530559229357" [3] "the sum of 0.16655966873187 and 0.349236445934426 is 0.515796114666296" [4] "the sum of 0.265324569774749 and 0.860124249616191 is 1.12544881939094" [5] "the sum of 0.890780709620268 and 0.404611144608053 is 1.29539185422832" [6] "the sum of -0.467888369466512 and 0.367044921399527 is -0.100843448066985" [7] "the sum of 0.758374556806851 and -1.51919904950798 is -0.760824492701126" ......... Is this your expected output? If not, give a clear example of what you are expecting. Could it be:> sum(a + b)[1] 16.90667 On Wed, Jun 19, 2013 at 8:16 PM, Thomas Parr <thomas.parr@maine.edu> wrote:> 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@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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. [[alternative HTML version deleted]]
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.