[R] evil attributes

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Mon Apr 12 01:57:53 CEST 2021


On 11/04/2021 2:46 p.m., Viechtbauer, Wolfgang (SP) wrote:
> The is.vector() thing has also bitten me in the behind on a few occasions. When I want to check if something is a vector, allow for it to possibly have some additional attributes (besides names) that would make is.vector() evaluate to FALSE, but evaluate to FALSE for lists (since is.vector(list(a=1, b=2)) is TRUE -- which also wasn't what I had initially expected before reading the documentation), I use:
> 
> .is.vector <- function(x)
>     is.atomic(x) && !is.matrix(x) && !is.null(x)
> 
> This might also work:
> 
> .is.vector <- function(x)
>     is(x, "vector") && !is.list(x)
> 
> I am sure there are all kinds of edge (and probably also not so edge) cases where these also fail to work properly. Kinda curious if there are better approaches out there.

Sorry, but nobody has said what "properly" would be here.  How can an 
approach be better at something if you don't say what you want it to do?

The base::is.vector() definition looks fairly useless, and I can't 
remember ever using that function.  But at least it's quite well 
documented what it is supposed to do.  What claims are you making about 
your .is.vector() definitions?

Duncan Murdoch



More information about the R-help mailing list