[R] evil attributes

Viechtbauer, Wolfgang (SP) wo||g@ng@v|echtb@uer @end|ng |rom m@@@tr|chtun|ver@|ty@n|
Sun Apr 11 20:46:02 CEST 2021


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.

Best,
Wolfgang

>-----Original Message-----
>From: R-help [mailto:r-help-bounces using r-project.org] On Behalf Of Therneau, Terry
>M., Ph.D. via R-help
>Sent: Saturday, 10 April, 2021 16:12
>To: R-help
>Subject: Re: [R] evil attributes
>
>I wrote: "I confess to being puzzled WHY the R core has decided on this
>definition..."
>After just a little more thought let me answer my own question.
>
>a. The as.vector() function is designed to strip off everything extraneous and
>leave just
>the core.   (I have a mental image of Jack Webb saying "Just the facts
>ma'am").   I myself
>use it freqently in the test suite for survival, in cases where I'm checking the
>corrent
>numeric result and don't care about any attached names.
>
>  b. is.vector(x) essentially answers the question "does x look like a result of
>as.vector?"
>
>Nevertheless I understand Roger's confusion.
>
>--
>Terry M Therneau, PhD
>Department of Quantitative Health Sciences
>Mayo Clinic
>therneau using mayo.edu
>
>"TERR-ree THUR-noh"


More information about the R-help mailing list