[R] evil attributes

Viechtbauer, Wolfgang (SP) wo||g@ng@v|echtb@uer @end|ng |rom m@@@tr|chtun|ver@|ty@n|
Mon Apr 12 14:09:13 CEST 2021

>-----Original Message-----
>From: Duncan Murdoch [mailto:murdoch.duncan using gmail.com]
>Sent: Monday, 12 April, 2021 1:58
>To: Viechtbauer, Wolfgang (SP); R-help
>Subject: Re: [R] evil attributes
>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

I only suggested a possible way of identifying vectors which is a bit more in line with the definition Bill described in his post (and which avoids the problem that Roger initially ran into that additional attributes make is.vector() evaluate to FALSE). It's not better than is.vector(), which is well documented as to what it does, and which has a different purpose. But I occasionally need to identify vectors (in the former sense) and was curious if there are other/better ways of doing so.


More information about the R-help mailing list