This is probably easy for experienced users but I could not find a solution. I have several R scripts that process several columns of a dataframe (several dataframes and columns actually, but simplified for my question). References such as: myDF$myCol are all over. I like to automate this for other dataframes and columns by defining a reference only once in the beginning of the script. One idea is to use strings: s1 <- "myDF" S2 <- "myCol" My question is how to construct the equivalent of myDF$myCol that can be used as such. Or is there a better solution? Thanks. The help and discussions on this forum are the best. Rene ----------------------------------------- Rene Braeckman, PhD Clinical Pharmacology & Pharmacometrics Irvine, California, USA
Dear Rene, There are several ways to do what you want, including get(s1)[,S2]. I hope this helps, John -------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 socserv.mcmaster.ca/jfox --------------------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Rene Braeckman > Sent: Sunday, February 04, 2007 3:43 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Reference to dataframe and contents > > This is probably easy for experienced users but I could not > find a solution. > > I have several R scripts that process several columns of a > dataframe (several dataframes and columns actually, but > simplified for my question). > > References such as: > > myDF$myCol > > are all over. I like to automate this for other dataframes > and columns by defining a reference only once in the > beginning of the script. > > One idea is to use strings: > > s1 <- "myDF" > S2 <- "myCol" > > My question is how to construct the equivalent of myDF$myCol > that can be used as such. Or is there a better solution? > > Thanks. The help and discussions on this forum are the best. > Rene > ----------------------------------------- > Rene Braeckman, PhD > Clinical Pharmacology & Pharmacometrics > Irvine, California, USA > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On Feb 4, 2007, at 3:42 PM, Rene Braeckman wrote:> My question is how to construct the equivalent of myDF$myCol that > can be > used as such. Or is there a better solution? >I think what you want is ?with and wrapping the whole work you want to do in a function.> Thanks. The help and discussions on this forum are the best. > ReneHaris
On 2/4/2007 3:42 PM, Rene Braeckman wrote:> This is probably easy for experienced users but I could not find a solution. > > I have several R scripts that process several columns of a dataframe > (several dataframes and columns actually, but simplified for my question). > > References such as: > > myDF$myCol > > are all over. I like to automate this for other dataframes and columns by > defining a reference only once in the beginning of the script. > > One idea is to use strings: > > s1 <- "myDF" > S2 <- "myCol" > > My question is how to construct the equivalent of myDF$myCol that can be > used as such. Or is there a better solution?I'd pass myDF and the name of the column as a parameters to the function, e.g. myfun <- function( DF, column ) { do something now with DF[, column] } then call it as myfun(myDF, "myCol") Duncan Murdoch> Thanks. The help and discussions on this forum are the best. > Rene > ----------------------------------------- > Rene Braeckman, PhD > Clinical Pharmacology & Pharmacometrics > Irvine, California, USA > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.