[R] Referencing data frames
Steven McKinney
smckinney at bccrc.ca
Sat Jun 13 04:35:29 CEST 2009
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.baz
X1 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.
More information about the R-help
mailing list