dansawyer
2014-May-29 20:13 UTC
[R] referencing a data frame with a pointer, link, alias, etc
Good afternoon. The question is: does R support pointer like structures? The specific problem is: a function supporting operations on a set of data frames with similar structures. I have a set of data frames with similar structures. I wish to perform similar operations on those data frames. I have successfully created and tested the operations and am ready to write a local function to perform them. To clarify the question below is a pigeon example for the question: df1 <- (col1, col2, ...) df2 <- (col1, col2, ...) function f(var) {var$col1 <- ... var$col2 <- ... } f(df1) Each of the data frames have common column names I would like for f to be able to perform operations on the data frames from within the function. I have tried testing variables without success. R allows for creating the necessary 'strings' with the correct names, however I am unable to bind, transform those strings to reference the data frames. -- View this message in context: http://r.789695.n4.nabble.com/referencing-a-data-frame-with-a-pointer-link-alias-etc-tp4691436.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2014-May-29 21:02 UTC
[R] referencing a data frame with a pointer, link, alias, etc
On May 29, 2014, at 1:13 PM, dansawyer wrote:> Good afternoon. > > The question is: does R support pointer like structures? The specific > problem is: > > a function supporting operations on a set of data frames with similar > structures. > > I have a set of data frames with similar structures. I wish to perform > similar operations on those data frames. I have successfully created and > tested the operations and am ready to write a local function to perform > them. To clarify the question below is a pigeon example for the question: > > df1 <- (col1, col2, ...) > df2 <- (col1, col2, ...)Not valid R code.> > function f(var) > {var$col1 <- ... > var$col2 <- ... > } > > f(df1) > > Each of the data frames have common column names > > I would like for f to be able to perform operations on the data frames from > within the function. I have tried testing variables without success. R > allows for creating the necessary 'strings' with the correct names, however > I am unable to bind, transform those strings to reference the data frames.Learn to use "[[" instead of "$". (There is a warning about not doing what you are doing in the help page for ?"$" .) And furthermore when you see this message (which Nabble rather impolitely removes) "and provide commented, minimal, self-contained, reproducible code." ... it means real R code and not pseudo-code as you are currently offering. David Winsemius Alameda, CA, USA
Jeff Newmiller
2014-May-29 21:10 UTC
[R] referencing a data frame with a pointer, link, alias, etc
Yes and no. There are "lists" that can hold anything, but for normal objects there are no "pass by reference" semantics. The exception to that is environments, but that is really a limited scope of usability. That is, you can receive a changed object in the calling scope and choose to overwrite your original variable if you choose. If you provided the "minimal reproducible example" asked for you might get some code examples in return. --------------------------------------------------------------------------- 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 May 29, 2014 1:13:10 PM PDT, dansawyer <dansawyer at earthlink.net> wrote:>Good afternoon. > >The question is: does R support pointer like structures? The specific >problem is: > >a function supporting operations on a set of data frames with similar >structures. > >I have a set of data frames with similar structures. I wish to perform >similar operations on those data frames. I have successfully created >and >tested the operations and am ready to write a local function to perform >them. To clarify the question below is a pigeon example for the >question: > >df1 <- (col1, col2, ...) >df2 <- (col1, col2, ...) > >function f(var) >{var$col1 <- ... >var$col2 <- ... >} > >f(df1) > >Each of the data frames have common column names > >I would like for f to be able to perform operations on the data frames >from >within the function. I have tried testing variables without success. R >allows for creating the necessary 'strings' with the correct names, >however >I am unable to bind, transform those strings to reference the data >frames. > > > >-- >View this message in context: >http://r.789695.n4.nabble.com/referencing-a-data-frame-with-a-pointer-link-alias-etc-tp4691436.html >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >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.
Jim Lemon
2014-May-29 23:00 UTC
[R] referencing a data frame with a pointer, link, alias, etc
On Thu, 29 May 2014 01:13:10 PM dansawyer wrote:> Good afternoon. > > The question is: does R support pointer like structures? The specific > problem is: > > a function supporting operations on a set of data frames with similar > structures. > > I have a set of data frames with similar structures. I wish to perform > similar operations on those data frames. I have successfully createdand> tested the operations and am ready to write a local function toperform> them. To clarify the question below is a pigeon example for thequestion:> > df1 <- (col1, col2, ...) > df2 <- (col1, col2, ...) > > function f(var) > {var$col1 <- ... > var$col2 <- ... > } > > f(df1) > > Each of the data frames have common column names > > I would like for f to be able to perform operations on the data framesfrom> within the function. I have tried testing variables without success. R > allows for creating the necessary 'strings' with the correct names,however> I am unable to bind, transform those strings to reference the dataframes.> >Hi dansawyer, I may not completely understand what you want to do, but I think you want the "get" function to sequentially process your dataframes. Maybe something like this: for(df in paste("df",1:10,sep="")) { var<-get(df) #do your processing on "var" } If you want to read in say, CSV files one at a time: for(df in paste("df",1:10,".csv",sep="")) { var<-read.csv(df) #do your processing on "var" } You may also want to define a class that specifies the structure of your dataframes. Jim