Hello, Consider the following transcript> x=NULL > x$date=10 > x$date [1] 10 or> x=NULL > x[10]=10 > x[1] NA NA NA NA NA NA NA NA NA 10 Wouldn't one expect that x remain NULL despite the further additions (i.e the x$date, x[10]) etc? Is coercing appropriate here? Cheers Saptarshi [[alternative HTML version deleted]]
Well, feel free to make up any semantics that you like for a language you write, but R already has its own and tells you what to expect:>From ?NULL:NULL can be indexed (see Extract) in just about any syntactically legal way: whether is makes sense or not, the result is always NULL. Objects with value NULL can be changed by replacement operators and will be coerced to the type of the right-hand side. Cheers, Bert On Fri, Jun 21, 2013 at 1:21 PM, Saptarshi Guha <saptarshi.guha at gmail.com> wrote:> Hello, > > Consider the following transcript > >> x=NULL >> x$date=10 >> x > $date > [1] 10 > > or > >> x=NULL >> x[10]=10 >> x > [1] NA NA NA NA NA NA NA NA NA 10 > > > Wouldn't one expect that x remain NULL despite the further additions (i.e > the x$date, x[10]) etc? Is coercing appropriate here? > > Cheers > Saptarshi > > [[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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
On Jun 21, 2013, at 1:21 PM, Saptarshi Guha wrote:> Hello, > > Consider the following transcript > >> x=NULL >> x$date=10 >> x > $date > [1] 10 > > or > >> x=NULL >> x[10]=10 >> x > [1] NA NA NA NA NA NA NA NA NA 10 > > > Wouldn't one expect that x remain NULL despite the further additions (i.e > the x$date, x[10]) etc? Is coercing appropriate here? >This seems like a bizarre expectation. You really want x to remain NULL until the end of time? Perhaps you meant to say that after creation of a tenth entry for x that you wanted x[1] to remain NULL (which it never was, since it didn't really exist). Or to have the display of missing values to be suppressed by the print function? Or were hoping the creation of an n-th item in x to _not_ create a vector of length n? Or that `x` be different than a multi-valued version of x[n]?> x=NULL > x[10]=10 > length(x)[1] 10> object.size(x)168 bytes> x[1000] <- 1000 > object.size(x)8040 bytes So you have apparently not realized that lists in R are actually vectors. .... by design. And the indexing a NULL value gives NULL rather than missing (NA): x <- NULL> xNULL> x[1]NULL> x[2]NULL> xNULL> x[10]=10 > x[1][1] NA Actually I think the behavior for "[" when indexing is attempted with arguments greater than the length of a list or vector is not at all what I would have expected. That it would have seem to me to have deserved a NULL return:> x[111000][1] NA -- David Winsemius Alameda, CA, USA