Chel Hee Lee
2015-Jan-29 15:18 UTC
[R] Passing a Data Frame Name as a Variable in a Function
I like Jeff's comments on the previous post.
Regarding Alan's question, please see the following example.
> df.1 <- data.frame(v1=1:5, v2=letters[1:5])
> df.2 <- data.frame(v1=LETTERS[1:3], v2=11:13)
> DFName <- ls(pattern = glob2rx("df.*"))[1]
> DFName
[1] "df.1"
> length(DFName[,1])
Error in DFName[, 1] : incorrect number of dimensions
'DFName' is a character vector of length 1 (it is neither a matrix nor a
data frame). In this case, you may try 'eval()' as below:
> eval(parse(text=DFName))
v1 v2
1 1 a
2 2 b
3 3 c
4 4 d
5 5 e
> eval(parse(text=DFName))[,1]
[1] 1 2 3 4 5
> length(eval(parse(text=DFName))[,1])
[1] 5
>
Is this what you are looking for? I hope this helps.
Chel Hee Lee
On 1/29/2015 12:34 AM, Jeff Newmiller wrote:> This approach is fraught with dangers.
>
> I recommend that you put all of those data frames into a list and have your
function accept the list and the name and use the list indexing operator
mylist[[DFName]] to refer to it. Having functions that go fishing around in the
global environment will be hard to maintain at best, and buggy at worst.
>
> That said, I usually work with all of my data frames combined as one and
use the plyr, dplyr, or data.table packages to apply my algorithms to each group
of rows identified by a character or factor column.
> ---------------------------------------------------------------------------
> Jeff Newmiller The ..... ..... Go Live...
> DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#.
Live Go...
> Live: OO#.. Dead: OO#.. Playing
> Research Engineer (Solar/Batteries O.O#. #.O#. with
> /Software/Embedded Controllers) .OO#. .OO#. rocks...1k
> ---------------------------------------------------------------------------
> Sent from my phone. Please excuse my brevity.
>
> On January 28, 2015 5:37:34 PM PST, Alan Yong <alanyong at
caltech.edu> wrote:
>> Dear R-help,
>> I have df.1001 as a data frame with rows & columns of values.
>>
>> I also have other data frames named similarly, i.e., df.*.
>>
>> I used DFName from:
>>
>> DFName <- ls(pattern = glob2rx("df.*"))[1]
>>
>> & would like to pass on DFName to another function, like:
>>
>> length(DFName[, 1])
>>
>> however, when I run:
>>
>>> length(DFName[, 1])
>> Error in DFName[, 1] : incorrect number of dimensions
>>
>> and
>>
>> length(df.1001[, 1])
>> [1] 104
>>
>> do not provide the same expected answer.
>>
>> How can I successfully pass the data frame name of df.1001 as a
>> variable named DFName in a function?
>>
>> Thanks,
>> Alan
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
David L Carlson
2015-Jan-29 17:28 UTC
[R] Passing a Data Frame Name as a Variable in a Function
That's fine, but I'm here in town if you want me to pick her up at the
airport.
David
-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Chel Hee Lee
Sent: Thursday, January 29, 2015 9:18 AM
To: Jeff Newmiller; Alan Yong; r-help at r-project.org
Subject: Re: [R] Passing a Data Frame Name as a Variable in a Function
I like Jeff's comments on the previous post.
Regarding Alan's question, please see the following example.
> df.1 <- data.frame(v1=1:5, v2=letters[1:5])
> df.2 <- data.frame(v1=LETTERS[1:3], v2=11:13)
> DFName <- ls(pattern = glob2rx("df.*"))[1]
> DFName
[1] "df.1"
> length(DFName[,1])
Error in DFName[, 1] : incorrect number of dimensions
'DFName' is a character vector of length 1 (it is neither a matrix nor a
data frame). In this case, you may try 'eval()' as below:
> eval(parse(text=DFName))
v1 v2
1 1 a
2 2 b
3 3 c
4 4 d
5 5 e
> eval(parse(text=DFName))[,1]
[1] 1 2 3 4 5
> length(eval(parse(text=DFName))[,1])
[1] 5
>
Is this what you are looking for? I hope this helps.
Chel Hee Lee
On 1/29/2015 12:34 AM, Jeff Newmiller wrote:> This approach is fraught with dangers.
>
> I recommend that you put all of those data frames into a list and have your
function accept the list and the name and use the list indexing operator
mylist[[DFName]] to refer to it. Having functions that go fishing around in the
global environment will be hard to maintain at best, and buggy at worst.
>
> That said, I usually work with all of my data frames combined as one and
use the plyr, dplyr, or data.table packages to apply my algorithms to each group
of rows identified by a character or factor column.
> ---------------------------------------------------------------------------
> Jeff Newmiller The ..... ..... Go Live...
> DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#.
Live Go...
> Live: OO#.. Dead: OO#.. Playing
> Research Engineer (Solar/Batteries O.O#. #.O#. with
> /Software/Embedded Controllers) .OO#. .OO#. rocks...1k
> ---------------------------------------------------------------------------
> Sent from my phone. Please excuse my brevity.
>
> On January 28, 2015 5:37:34 PM PST, Alan Yong <alanyong at
caltech.edu> wrote:
>> Dear R-help,
>> I have df.1001 as a data frame with rows & columns of values.
>>
>> I also have other data frames named similarly, i.e., df.*.
>>
>> I used DFName from:
>>
>> DFName <- ls(pattern = glob2rx("df.*"))[1]
>>
>> & would like to pass on DFName to another function, like:
>>
>> length(DFName[, 1])
>>
>> however, when I run:
>>
>>> length(DFName[, 1])
>> Error in DFName[, 1] : incorrect number of dimensions
>>
>> and
>>
>> length(df.1001[, 1])
>> [1] 104
>>
>> do not provide the same expected answer.
>>
>> How can I successfully pass the data frame name of df.1001 as a
>> variable named DFName in a function?
>>
>> Thanks,
>> Alan
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.
Yes. I thought I was replying to a different message. Sorry. David -----Original Message----- From: Chel Hee Lee [mailto:chl948 at mail.usask.ca] Sent: Thursday, January 29, 2015 11:33 AM To: David L Carlson Subject: Your personal email on the R-help mail list Hi David, I am not sure if you noticed that your personal conversation is on the R-help mailing list. Chel Hee Lee, PhD Biostatistician and Manager Clinical Research Support Unit College of Medicine University of Saskatchewan Canada On 1/29/2015 11:28 AM, David L Carlson wrote:> That's fine, but I'm here in town if you want me to pick her up at the airport. > > David > > -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Chel Hee Lee > Sent: Thursday, January 29, 2015 9:18 AM > To: Jeff Newmiller; Alan Yong; r-help at r-project.org > Subject: Re: [R] Passing a Data Frame Name as a Variable in a Function > > I like Jeff's comments on the previous post. > > Regarding Alan's question, please see the following example. > > > df.1 <- data.frame(v1=1:5, v2=letters[1:5]) > > df.2 <- data.frame(v1=LETTERS[1:3], v2=11:13) > > DFName <- ls(pattern = glob2rx("df.*"))[1] > > DFName > [1] "df.1" > > length(DFName[,1]) > Error in DFName[, 1] : incorrect number of dimensions > > 'DFName' is a character vector of length 1 (it is neither a matrix nor a > data frame). In this case, you may try 'eval()' as below: > > > eval(parse(text=DFName)) > v1 v2 > 1 1 a > 2 2 b > 3 3 c > 4 4 d > 5 5 e > > eval(parse(text=DFName))[,1] > [1] 1 2 3 4 5 > > length(eval(parse(text=DFName))[,1]) > [1] 5 > > > > Is this what you are looking for? I hope this helps. > > Chel Hee Lee > > > On 1/29/2015 12:34 AM, Jeff Newmiller wrote: >> This approach is fraught with dangers. >> >> I recommend that you put all of those data frames into a list and have your function accept the list and the name and use the list indexing operator mylist[[DFName]] to refer to it. Having functions that go fishing around in the global environment will be hard to maintain at best, and buggy at worst. >> >> That said, I usually work with all of my data frames combined as one and use the plyr, dplyr, or data.table packages to apply my algorithms to each group of rows identified by a character or factor column. >> --------------------------------------------------------------------------- >> Jeff Newmiller The ..... ..... Go Live... >> DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... >> Live: OO#.. Dead: OO#.. Playing >> Research Engineer (Solar/Batteries O.O#. #.O#. with >> /Software/Embedded Controllers) .OO#. .OO#. rocks...1k >> --------------------------------------------------------------------------- >> Sent from my phone. Please excuse my brevity. >> >> On January 28, 2015 5:37:34 PM PST, Alan Yong <alanyong at caltech.edu> wrote: >>> Dear R-help, >>> I have df.1001 as a data frame with rows & columns of values. >>> >>> I also have other data frames named similarly, i.e., df.*. >>> >>> I used DFName from: >>> >>> DFName <- ls(pattern = glob2rx("df.*"))[1] >>> >>> & would like to pass on DFName to another function, like: >>> >>> length(DFName[, 1]) >>> >>> however, when I run: >>> >>>> length(DFName[, 1]) >>> Error in DFName[, 1] : incorrect number of dimensions >>> >>> and >>> >>> length(df.1001[, 1]) >>> [1] 104 >>> >>> do not provide the same expected answer. >>> >>> How can I successfully pass the data frame name of df.1001 as a >>> variable named DFName in a function? >>> >>> Thanks, >>> Alan >>> >>> ______________________________________________ >>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> 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. >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. >> > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >