# Testing for vectors

11 messages
Open this post in threaded view
|

## Testing for vectors

 Hi all, Is there are base function that I've missed that tests if an object is a vector in the dimensionality sense, rather than the data structure sense? i.e. something that checks is.null(dim(x)) ? is.vector() is trivially disqualified since it also checks for the presence of non-names attributes: x <- factor(c("a", "a", "b")) is.vector(x) #> [1] FALSE is.null(dim(x)) #> [1] TRUE Hadley -- http://hadley.nz______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: Testing for vectors

 On 07/07/2018 1:20 PM, Hadley Wickham wrote: > Hi all, > > Is there are base function that I've missed that tests if an object is > a vector in the dimensionality sense, rather than the data structure > sense? i.e. something that checks is.null(dim(x)) ? > > is.vector() is trivially disqualified since it also checks for the > presence of non-names attributes: > > x <- factor(c("a", "a", "b")) > is.vector(x) > #> [1] FALSE > > is.null(dim(x)) > #> [1] TRUE > I don't know of one.  I can't think of nontrivial cases where that distinction matters; do you know of any where base functions act differently on vectors and 1D arrays?  (A trivial example is that dimnames(x) gives different results for a named vector and an array with dimnames.) Duncan Murdoch ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: Testing for vectors

 On Sat, Jul 7, 2018 at 12:54 PM, Duncan Murdoch <[hidden email]> wrote: > On 07/07/2018 1:20 PM, Hadley Wickham wrote: >> >> Hi all, >> >> Is there are base function that I've missed that tests if an object is >> a vector in the dimensionality sense, rather than the data structure >> sense? i.e. something that checks is.null(dim(x)) ? >> >> is.vector() is trivially disqualified since it also checks for the >> presence of non-names attributes: >> >> x <- factor(c("a", "a", "b")) >> is.vector(x) >> #> [1] FALSE >> >> is.null(dim(x)) >> #> [1] TRUE >> > > I don't know of one.  I can't think of nontrivial cases where that > distinction matters; do you know of any where base functions act differently > on vectors and 1D arrays?  (A trivial example is that dimnames(x) gives > different results for a named vector and an array with dimnames.) I was thinking primarily of completing the set of is.matrix() and is.array(), or generally, how do you say: is `x` a 1d dimensional thing? (I don't have any feel for whether the check should be is.null(dim(x)) vs. length(dim(x)) <= 1) Hadley -- http://hadley.nz______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: Testing for vectors

 Hadley, > I was thinking primarily of completing the set of is.matrix() and > is.array(), or generally, how do you say: is `x` a 1d dimensional > thing? > Can you clarify what you mean by dimensionality sense and specifically 1d here? You can have a 1d array which is different from what your proposed function would call a vector. So is.null(dim(x)) doesn't seem the same as 1d, right? > x = array(1:10) > x  [1]  1  2  3  4  5  6  7  8  9 10 > class(x) [1] "array" > dim(x) [1] 10 > dim(1:10) NULL You can also have an n x 1 matrix, which *technically* has 2 dimensions but conceptually is equivalent to a 1d array and/or a vector. Also, are you including lists in your conceptions of 1d vector here? I'm with Duncan here, in that i'm having trouble understanding exactly what you want to do without a bit more context. Best, ~G > > (I don't have any feel for whether the check should be is.null(dim(x)) > vs. length(dim(x)) <= 1) > > Hadley > -- > http://hadley.nz> > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel> > -- Gabriel Becker, Ph.D Scientist Bioinformatics and Computational Biology Genentech Research         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: Testing for vectors

 On Sat, Jul 7, 2018 at 1:50 PM, Gabe Becker <[hidden email]> wrote: > Hadley, > >> >> I was thinking primarily of completing the set of is.matrix() and >> is.array(), or generally, how do you say: is `x` a 1d dimensional >> thing? > > > Can you clarify what you mean by dimensionality sense and specifically 1d > here? What do we call a vector that is not an array? (or matrix) What do we call an object that acts 1-dimensional? (i.e. has length(dim()) %in% c(0, 1)) ? > You can also have an n x 1 matrix, which technically has 2 dimensions but > conceptually is equivalent to a 1d array and/or a vector. Yes. You can also have array that's n x 1 x 1. > Also, are you including lists in your conceptions of 1d vector here? I'm > with Duncan here, in that i'm having trouble understanding exactly what you > want to do without a bit more context. Isn't it standard terminology that a vector is the set of atomic vectors + list? Hadley -- http://hadley.nz______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: Testing for vectors

 Hello, Inline. Às 21:32 de 07-07-2018, Hadley Wickham escreveu: > On Sat, Jul 7, 2018 at 1:50 PM, Gabe Becker <[hidden email]> wrote: >> Hadley, >> >>> >>> I was thinking primarily of completing the set of is.matrix() and >>> is.array(), or generally, how do you say: is `x` a 1d dimensional >>> thing? >> >> >> Can you clarify what you mean by dimensionality sense and specifically 1d >> here? > > What do we call a vector that is not an array? (or matrix) > > What do we call an object that acts 1-dimensional? (i.e. has > length(dim()) %in% c(0, 1)) ? > >> You can also have an n x 1 matrix, which technically has 2 dimensions but >> conceptually is equivalent to a 1d array and/or a vector. > > Yes. You can also have array that's n x 1 x 1. In which case it would be (length(dim(.)) - sum(dim(.) == 1)) %in% c(0, 1) Hope this helps, Rui Barradas > >> Also, are you including lists in your conceptions of 1d vector here? I'm >> with Duncan here, in that i'm having trouble understanding exactly what you >> want to do without a bit more context. > > Isn't it standard terminology that a vector is the set of atomic vectors + list? > > Hadley > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: Testing for vectors

 In reply to this post by hadley wickham Thanks, Hadley for bringing this up:-) I am teaching R and I can suggest 5 different definitions of 'vector': a) vector as a collection of homogeneous objects, indexed by [ ] (more precisely atomic vector).  Sometimes you hear that in R, "everything is a vector", but this is only true for atomic objects. b) vector as a collection of objects, indexed by either [ ] and [[ ]]. This includes atomic vectors and lists. c) vector versus scalar.  It pops up when teaching math and stats, and is somewhat confusing, in particular if my previous claim was that "R does not have scalars". d) vector versus matrix (or other arrays).  Again, it only matters when doing matrix operations where 'vectors', i.e. objects with NULL dimension, behave their own way. e) finally, 'is.vector' has it's own understanding what constitutes a vector. Maybe there are more...  I don't think there are any easy answers but both me and my students would appreciate more consistent terminology. Cheers, Ott On Sat, Jul 7, 2018 at 1:32 PM, Hadley Wickham <[hidden email]> wrote: > On Sat, Jul 7, 2018 at 1:50 PM, Gabe Becker <[hidden email]> wrote: > > Hadley, > > > >> > >> I was thinking primarily of completing the set of is.matrix() and > >> is.array(), or generally, how do you say: is `x` a 1d dimensional > >> thing? > > > > > > Can you clarify what you mean by dimensionality sense and specifically 1d > > here? > > What do we call a vector that is not an array? (or matrix) > > What do we call an object that acts 1-dimensional? (i.e. has > length(dim()) %in% c(0, 1)) ? > > > You can also have an n x 1 matrix, which technically has 2 dimensions but > > conceptually is equivalent to a 1d array and/or a vector. > > Yes. You can also have array that's n x 1 x 1. > > > Also, are you including lists in your conceptions of 1d vector here? I'm > > with Duncan here, in that i'm having trouble understanding exactly what > you > > want to do without a bit more context. > > Isn't it standard terminology that a vector is the set of atomic vectors + > list? > > Hadley > > -- > http://hadley.nz> > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel>         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: Testing for vectors

 In reply to this post by hadley wickham Hadley, On Sat, Jul 7, 2018 at 1:32 PM, Hadley Wickham <[hidden email]> wrote: > On Sat, Jul 7, 2018 at 1:50 PM, Gabe Becker <[hidden email]> wrote: > > Hadley, > > > >> > >> I was thinking primarily of completing the set of is.matrix() and > >> is.array(), or generally, how do you say: is `x` a 1d dimensional > >> thing? > > > > > > Can you clarify what you mean by dimensionality sense and specifically 1d > > here? > > What do we call a vector that is not an array? (or matrix) > > What do we call an object that acts 1-dimensional? (i.e. has > length(dim()) %in% c(0, 1)) ? > Right, or even (length(dim()) == 0 || sum(dim() > 1) <= 1)  but that is exactly my point, those two(/three) sets of things are not the same. 1d arrays meet the second definition but not the first. Matrices and arrays that don't meet either of yours would still meet mine. Which definition are you proposing strictly define what a vector is? Before we have a function which performs the test we need to actually know what is being tested. So again I would mirror Duncan's question: What code do you have now or are you planning to write that needs to check this, and how would it feel about the various types of objects being discussed here? In other words, in what cases do you actually need to strictly check if something "is a vector"? Another completely unrelated way to define vector, btw, is via the vector interface (from what I recall this is roughly [, [[, length, and format methods, though I'm probably forgetting some). This is (more or less) equivalent to defining a vector as "a thing that can be the column of a data.frame and have all the base-provided machinery work". > You can also have an n x 1 matrix, which technically has 2 dimensions but > > conceptually is equivalent to a 1d array and/or a vector. > > Yes. You can also have array that's n x 1 x 1. > > > Also, are you including lists in your conceptions of 1d vector here? I'm > > with Duncan here, in that i'm having trouble understanding exactly what > you > > want to do without a bit more context. > > Isn't it standard terminology that a vector is the set of atomic vectors + > list? > Maybe. If by standard you mean commonly used/understood, though, I doubt most R users would understand a list to be a vector. I think most people think of atomic vectors exclusively when they hear "vector" unless they've very specifically been trained not to do so. Best, ~G > > Hadley > > -- > http://hadley.nz> -- Gabriel Becker, Ph.D Scientist Bioinformatics and Computational Biology Genentech Research         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: Testing for vectors

 Gabe, I agree that If by standard you mean commonly used/understood, though, I doubt > most R users would understand a list to be a vector. I think most people > think of atomic vectors exclusively when they hear "vector" unless they've > very specifically been trained not to do so. However, a common way to create lists is by a construct like 'vector("list", n)'.  Also, 'is.vector' reports TRUE for lists (but FALSE for many other vectors).  This causes quite a bit of confusion, unless everyone understands the different 'vector' concepts, embedded in R. Cheers, Ott         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel