[R] Cause of error assigning NULL to element of a vector
Marc Schwartz
marc_schwartz at me.com
Wed May 24 20:51:41 CEST 2017
> On May 24, 2017, at 1:30 PM, Ramnik Bansal <ramnik.bansal at gmail.com> wrote:
>
> What is the cause of the error below ?
>
>> y <- 1
>> y[1] <- NULL
> Error in y[1] <- NULL : replacement has length zero
>
> Thanks,
> Ramnik
Hi,
> length(NULL)
[1] 0
You are attempting to assign NULL, which is a zero length special object of its own kind, to a specific element in the vector object 'y' and that cannot be done.
You can assign NULL to 'y':
y <- NULL
> y
NULL
> length(y)
[1] 0
BUT, 'y' is not a vector in that case:
> is.vector(y)
[1] FALSE
Nor can a vector contain multiple NULLs:
y <- c(NULL, NULL, NULL)
> y
NULL
> length(y)
[1] 0
Similarly:
y <- c(1, NULL, 1)
> y
[1] 1 1
> length(y)
[1] 2
Perhaps reviewing ?NULL as well as:
https://cran.r-project.org/doc/manuals/r-release/R-lang.html#NULL-object
might also provide some insights.
Regards,
Marc Schwartz
More information about the R-help
mailing list