[R] Passing a Data Frame Name as a Variable in a Function
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@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.
Re: [R] Passing a Data Frame Name as a Variable in a Function
Much thanks to everyone for their recommendations! I agree that fishing in the global environment isn't ideal & only shows my budding understanding of R. For now, I will adapt Chel Hee's "length(eval(parse(text=DFName))[,1])" solution then fully explore Jeff's suggestion to put the data frames into a list. At best, I am implementing an ineloquent approach to: (1) Add a column to each data frame with a string that is parsed from the appendage of the data frame name, i.e., string is "1001" from data frame object of "df.1001"; then, (2) Bind the rows of all the files. Any more feedback will be much appreciated! Cheers, Alan On Jan 29, 2015, at 3:11 AM, peter dalgaard wrote: > > On 29 Jan 2015, at 07:34 , 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. > > Agreed. However, just to help understand the issue: > > DFName is a length-one vector of character strings, not the object that has > the name contained in the string. I.e. you can do nchar(DFName) and > presumably get the value 7, but there is no operation on df.1001 that can > tell you the length of its name. You can (but shouldn't, as per Jeff's note) > get from name to object using get()/assign() and also via some concoctions > involving combinations of eval(), parse(), as.name(), and substitute(). > > -- > Peter Dalgaard, Professor, > Center for Statistics, Copenhagen Business School > Solbjerg Plads 3, 2000 Frederiksberg, Denmark > Phone: (+45)38153501 > Email: pd@cbs.dk Priv: pda...@gmail.com > > > > > > > > __ R-help@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.
Re: [R] Passing a Data Frame Name as a Variable in a Function
Much thanks, Hadley!
Cheers,
Alan
On Jan 29, 2015, at 12:36 PM, Hadley Wickham wrote:
> On Thu, Jan 29, 2015 at 11:43 AM, Alan Yong wrote:
>> Much thanks to everyone for their recommendations! I agree that fishing in
>> the global environment isn't ideal & only shows my budding understanding of
>> R.
>>
>> For now, I will adapt Chel Hee's "length(eval(parse(text=DFName))[,1])"
>> solution then fully explore Jeff's suggestion to put the data frames into a
>> list.
>
> If you have to go down this route, at least do nrow(get(DFName))
>
>> (1) Add a column to each data frame with a string that is parsed from the
>> appendage of the data frame name, i.e., string is "1001" from data frame
>> object of "df.1001"; then,
>> (2) Bind the rows of all the files.
>
> I'd highly recommend learning a little functional programming such as
> the use of lapply (e.g. http://adv-r.had.co.nz/Functionals.html).
> Then you can easily do:
>
> csvs <- dir(pattern = "\\.csv$")
> all <- lapply(csvs, read.csv)
> one <- do.call("rbind", all)
>
> to find all the csv files in a directory, load into a list and then
> collapse into a single data frame.
>
> You're much better off learning how to do this than futzing around
> with named objects in the global environment.
>
> Hadley
>
> --
> http://had.co.nz/
__
R-help@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.

