Hi, How do I use the string content of a string variable to reference a data frame of the same name? I want to do the typical tasks of 1) building a name with a string variable and using the string variable to create a data frame (or any object) whose name is the string value of the variable and 2) pass on a string to a function as a parameter, and then use that string to refer to an existing data frame. Thanks in advance. Payam -- Payam Minoofar, Ph.D. Scientist Meissner Filtration Products 4181 Calle Tesoro Camarillo, CA 93012 USA +1 805 388 9911 +1 805 388 5948 fax Payam.minoofar@meissner.com [[alternative HTML version deleted]]
On 12/06/2009 6:00 PM, Payam Minoofar wrote:> Hi, > > How do I use the string content of a string variable to reference a data frame of the same name? I want to do the typical tasks of 1) building a name with a string variable and using the string variable to create a data frame (or any object) whose name is the string value of the variable and 2) pass on a string to a function as a parameter, and then use that string to refer to an existing data frame. > > Thanks in advance.assign() and get() do the basic bits. They aren't specific to dataframes. Duncan Murdoch> > Payam > -- > Payam Minoofar, Ph.D. > Scientist > Meissner Filtration Products > 4181 Calle Tesoro > Camarillo, CA 93012 > USA > +1 805 388 9911 > +1 805 388 5948 fax > Payam.minoofar at meissner.com > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Hi Payam, Here's a basic example using pointless data frames and an otherwise useless function to illustrate the issues you want> ## Fresh R session with nothing yet defined > foo <- data.frame(matrix(1:12, ncol = 3)) > bar <- data.frame(matrix(101:112, ncol = 3)) > > objects()[1] "bar" "foo"> > myrbind <- function(names) {+ df1 <- get(names[1]) + df2 <- get(names[2]) + dfrbind <- rbind(df1, df2) + dfrbind.name <- paste(names, collapse = ".") + assign(dfrbind.name, dfrbind, pos = 1) + invisible() + }> > dfnames <- c("foo", "bar", "baz") > myrbind(dfnames) > objects()[1] "bar" "dfnames" "foo" "foo.bar.baz" "myrbind"> foo.bar.bazX1 X2 X3 1 1 5 9 2 2 6 10 3 3 7 11 4 4 8 12 5 101 105 109 6 102 106 110 7 103 107 111 8 104 108 112>HTH Steven McKinney, Ph.D. Statistician Molecular Oncology and Breast Cancer Program British Columbia Cancer Research Centre email: smckinney +at+ bccrc +dot+ ca tel: 604-675-8000 x7561 BCCRC Molecular Oncology 675 West 10th Ave, Floor 4 Vancouver B.C. V5Z 1L3 Canada -----Original Message----- From: r-help-bounces at r-project.org on behalf of Payam Minoofar Sent: Fri 6/12/2009 3:00 PM To: r-help at r-project.org Subject: [R] Referencing data frames Hi, How do I use the string content of a string variable to reference a data frame of the same name? I want to do the typical tasks of 1) building a name with a string variable and using the string variable to create a data frame (or any object) whose name is the string value of the variable and 2) pass on a string to a function as a parameter, and then use that string to refer to an existing data frame. Thanks in advance. Payam -- Payam Minoofar, Ph.D. Scientist Meissner Filtration Products 4181 Calle Tesoro Camarillo, CA 93012 USA +1 805 388 9911 +1 805 388 5948 fax Payam.minoofar at meissner.com [[alternative HTML version deleted]] ______________________________________________ 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.
Generally when someone asks a question like this, they are trying to program in a different language using R rather than taking advantage of the power of R itself. If you give us more information on what you are trying to accomplish, then we may be able to give better advice on how to accomplish your final goal using the power of R. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Payam Minoofar > Sent: Friday, June 12, 2009 4:00 PM > To: r-help at r-project.org > Subject: [R] Referencing data frames > > Hi, > > How do I use the string content of a string variable to reference a > data frame of the same name? I want to do the typical tasks of 1) > building a name with a string variable and using the string variable to > create a data frame (or any object) whose name is the string value of > the variable and 2) pass on a string to a function as a parameter, and > then use that string to refer to an existing data frame. > > Thanks in advance. > > Payam > -- > Payam Minoofar, Ph.D. > Scientist > Meissner Filtration Products > 4181 Calle Tesoro > Camarillo, CA 93012 > USA > +1 805 388 9911 > +1 805 388 5948 fax > Payam.minoofar at meissner.com > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.