[R] Create a new variable and concatenation inside a "for" loop

Rolf Turner r.turner at auckland.ac.nz
Tue May 3 01:54:49 CEST 2016



You are quite wrong.  This is not "nonsense"; it is sound advice.  It is 
true that R is capable of distinguishing between a function and a 
non-function object with the same name.  However there are many 
circumstances in which using a function's name for the name of a data 
object will lead to errors in function calls and the resulting error 
messages will be difficult to interpret.  (Things like "object of type 
'builtin' is not subsettable".)

Indulging in using "c" (e.g.) for the name of a data object is bad 
practice and mental laziness.

I would suggest that you restrain your presumptuousness and arrogance, 
and refrain from telling experienced and wise R gurus, like Jeff 
Newmiller, that their ideas are "nonsense" until you have a little more 
experience with and knowledge of R.

I am sure that you will insist on arguing about this and that you will 
continue to claim that black is white, but the fact remains that you are 
wrong.  Others should pay no attention to your ranting.

cheers,

Rolf Turner

-- 
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

On 03/05/16 10:00, Raubertas, Richard wrote:
> What nonsense. There is a group of finger-waggers on this list who
> jump on every poster who uses the name of an R function as a variable
> name. R is perfectly capable of distinguishing the two, so if 'c' (or
> 'data' or 'df', etc.) is the natural name for a variable then go ahead
> and use it. Mr. Newmiller provides an excellent example of this: he
> recommends 'C' instead of 'c', apparently without realizing that 'C' is
> also a built-in R function--because there is "no such problem".
>
> Richard Raubertas
>
> -----Original Message----- From: R-help
> [mailto:r-help-bounces at r-project.org] On Behalf Of JeffNewmiller
> Sent: Wednesday, April 27, 2016 3:58 PM To: Gordon, Fabiana;
> 'r-help at R-project.org' Subject: Re: [R] Create a new variable and
> concatenation inside a  "for" loop
> "c" an extremely commonly-used function. Functions are first-class
> objects that occupy the same namespaces that variables do, so they can
> obscure each other. In short, don't use variables called "c" (R is case
> sensitive, so "C" has no such problem).
>
> Wherever possible, avoid incremental concatenation like the plague.
> If  you feel you must use it, at least concatenate in lists and then use
> functions like unlist, do.call, or pre-allocate vectors or matrix-like
> objects with unuseful values like NA and then overwrite each element in
> the vector or matrix-type object in a loop like your first one.



More information about the R-help mailing list