[R] Best Programming Practices regarding data frames

R. Michael Weylandt michael.weylandt at gmail.com
Thu Aug 2 00:13:01 CEST 2012


On Wed, Aug 1, 2012 at 4:19 PM, Ramiro Barrantes
<ramiro at precisionbioassay.com> wrote:
> Hello,
>
> I come from using different programming languages (C++, Mathematica, Perl) but have been using R extensively for several months.  I see the data frame as a key piece of the language and wanted to inquire people's experience regarding its use.
>
> Say you have a data frame D
>
> D <- data.frame(some columns)
>
> and you define a function that needs the information from this data frame and is supposed to return a calculation based on some columns of such data frame D.
>
> func <- function(d) {}
> #EFFECT: Does calculation X from some columns of d
>
> QUESTION: Would you consider better practice to return the same data.frame but expanded, or would you return a small data frame that consists of the newly computed columns?

I'd say return what you need, no more no less: and if you want to
reattach it to the input data, do that at the caller level, but don't
make it required: orthogonality and minimality and all that jazz....

As Bert points out, note that returning a data.frame is by no means
necessary -- they aren't "primitive" data structures like (atomic)
vectors and lists [we are in a Scheme dialect after all!], but they
are helpful and well supported. Use them liberally but no more than
necessary ;-)

Best,
Michael

>
> Some might say, either way, personal preference.  But after using and seeing other's code for some time, I am thinking that returning the result that consists of ONLY the relevant columns is a better practice as it defines the function as only returning what it was intended to return, and leaves it up to the user of the function to do whatever they were intending to do with it (including naming of the new columns, adding them to a data frame, etc.).  This might be a question for a computer programming theory group, but if anybody has any insight from their experience please share.
>
> Thanks in advance,
>
> Ramiro
>
>         [[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