Re: [R] evil attributes
>-Original Message- >From: Duncan Murdoch [mailto:murdoch.dun...@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. Best, Wolfgang __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] evil attributes
Terry wrote I confess to being puzzled WHY the R core has decided on this definition [of vector] ... I believe that "R core" followed S's definition of "vector". From the beginning (at least when I first saw it in 1981) an S vector was the basic unit of an S object - it had a type and a length and no more. This has little to do with the mathematician's or physicist's notion of a vector. It is more like what Technopedia ( https://www.techopedia.com/definition/22817/vector-programming) says is a programmer's notion of a vector: What Does Vector Mean? A vector, in programming, is a type of array that is one dimensional. Vectors are a logical element in programming languages that are used for storing data. Vectors are similar to arrays but their actual implementation and operation differs. Techopedia Explains Vector Vectors are primarily used within the programming context of most programming languages and serve as data structure containers. Being a data structure, vectors are used for storing objects and collections of objects in an organized structure. The major difference between and array and a vector is that, unlike typical arrays, the container size of a vector can be easily increased and decreased to complement different data storage types. Vectors have a dynamic structure and provide the ability to assign container size up front and enable allocation of memory space quickly. Vectors can be thought of as dynamic arrays. -Bill On Sun, Apr 11, 2021 at 8:04 AM Therneau, Terry M., Ph.D. via R-help < r-help@r-project.org> wrote: > 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 > thern...@mayo.edu > > "TERR-ree THUR-noh" > > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
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 __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] evil attributes
On 4/11/21 11:46 AM, 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) That will allow expression vectors to return TRUE, but they are not atomic so they would be excluded by your current version. -- David. 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. You might want to exclude expression vectors as well. Best, Wolfgang -Original Message- From: R-help [mailto:r-help-boun...@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 thern...@mayo.edu "TERR-ree THUR-noh" __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] evil attributes
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-boun...@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 >thern...@mayo.edu > >"TERR-ree THUR-noh" __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
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 thern...@mayo.edu "TERR-ree THUR-noh" [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.