[R-sig-teaching] The pedagogy of the assignment operator

Douglas Bates bates at stat.wisc.edu
Tue Oct 26 21:43:07 CEST 2010


Arguments in favor of using "=" as the assignment operator:
 - it is familiar to any students who have studied compiled languages
like C++ or Java or interpreted languages like Matlab, Perl, Python,
...
 - it is easier to type than "<-"

Arguments in favor of using "<-"
 - "<-" is more general than "=".  They are almost but not quite
equivalent and the case where they do differ is something I use
frequently.

More detail:

I encourage students to use str() frequently to check the structure of
the data object that they are producing and make sure it corresponds
to what they think it should be.  Often I wrap an assignment in a call
to str() or to summary().  For example

str(read.csv("WRAP.csv"))
summary(wrap <- within(read.csv("WRAP.csv"),
                   {
                       DBID <- factor(DBID)
                       VisNo <- factor(VisNo, labels=c("base","follow"))
                       family <- factor(family)
                       famhist <- factor(famhist, labels=c("N","Y"))
                       apoe <- factor(apoe, labels=c("N","Y"))
                       gender <- factor(gender, labels=c("M","F"))
                       ed <- ordered(ed)
                       race <- factor(race)
                       tester <- factor(tester)
                   }))

The first call to str is to check what variables are recorded as
integers and should be factors.  The call to within performs the
desired transformations following which the data frame is assigned to
the name "wrap" and summarized.  You can't write that as

summary(wrap = within(...

because the "=" is taken to indicate a named argument, not assignment.


On Tue, Oct 26, 2010 at 2:10 PM, Murray Jorgensen <maj at waikato.ac.nz> wrote:
> Greetings all,
>
> in my own R code I have used various forms of the R assignment operator at
> different times in my life. There are arguments for and against each choice.
>
> A question I would like this sig is whether there are any specifically
> teaching reasons for preferring one form over another?
>
> A second question, mainly to clarify the first, is whether different forms
> might be preferred for different types of student?
>
> Awaiting responses with interest  -  Murray
>
> --
> Dr Murray Jorgensen      http://www.stats.waikato.ac.nz/Staff/maj.html
> Department of Statistics, University of Waikato, Hamilton, New Zealand
> Email: maj at waikato.ac.nz    majorgensen at ihug.co.nz      Fax 7 838 4155
> Phone  +64 7 838 4773 wk    Home +64 7 825 0441   Mobile 021 0200 8350
>
> _______________________________________________
> R-sig-teaching at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-teaching
>




More information about the R-sig-teaching mailing list