[R] Nulls being coerced : Bug or design?
David Winsemius
dwinsemius at comcast.net
Fri Jun 21 23:38:37 CEST 2013
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
> x
NULL
> x[1]
NULL
> x[2]
NULL
> x
NULL
> 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
More information about the R-help
mailing list