Hi Simon, thank you for the concise reply. Do you mean the reported behavior of drop() is not a bug? It looks like a borderline bug to me (see below), but I'm not the judge of that. If this is the intended behavior and serves an actual purpose, then that could be explicitly documented in a \note{} on the help page. Such a note would slightly reduce the surprise of users running into this behavior. This is related to the oddity that one-dimensional arrays are: array(month.abb, dim=c(1,1,12)) # array array(month.abb, dim=c(1,12)) # matrix array(month.abb, dim=12) # array Firstly, one would expect the pattern to be array-matrix-vector. Secondly, it's easy to drop() the three-dimensional and two-dimensional objects, but drop() does nothing to the one-dimensional array. Instead, it takes an unintuitive combination of methods to convert a single-dimensional to a vector, while retaining its names. Or I may well be missing something obvious. Best regards, Arni On Wed, 8 Sep 2010, Simon Urbanek wrote:> wrong address - did you mean R-devel? > Simon > > > > On Sep 6, 2010, at 8:35 AM, Arni Magnusson wrote: > >> Bug or not, I was surprised by this behavior: >> >> x <- tapply(chickwts$weight, chickwts$feed, median) >> x # array ... I'd like to convert to vector with named elements >> drop(x) # what, still an array? >> drop(as.matrix(x)) # this works >> drop(t(x)) # this works >> >> I was expecting drop(x) to return a vector, and I suspect many R users >> would too. The title in help(drop), "Drop Redundant Extent >> Information", suggests that such a simple array would be converted to a >> vector. >> >> Reading through the help page, I note that this is perhaps not a clear >> bug, but somewhat unclear behavior. >> >> The most compact way to break the vector out of its eggshell seems to >> be >> >> t(x)[,] >> >> but drop(x) would be much easier to read and write. There's nothing >> particularly matrix about x, so it's not obvious that the conversion >> should involve as.matrix(x). >> >> Thanks, >> >> Arni >
On 09/08/2010 08:54 PM, Arni Magnusson wrote:> Hi Simon, thank you for the concise reply. > > Do you mean the reported behavior of drop() is not a bug? > > It looks like a borderline bug to me (see below), but I'm not the judge of > that. If this is the intended behavior and serves an actual purpose, then > that could be explicitly documented in a \note{} on the help page. > > Such a note would slightly reduce the surprise of users running into this > behavior. > > This is related to the oddity that one-dimensional arrays are: > > array(month.abb, dim=c(1,1,12)) # array > array(month.abb, dim=c(1,12)) # matrix > array(month.abb, dim=12) # array > > Firstly, one would expect the pattern to be array-matrix-vector. Secondly, > it's easy to drop() the three-dimensional and two-dimensional objects, but > drop() does nothing to the one-dimensional array. Instead, it takes an > unintuitive combination of methods to convert a single-dimensional to a > vector, while retaining its names. Or I may well be missing something > obvious.Well, you're missing c(x), but you may still have a point... 1d arrays was something of an afterthought, introduced for the ability to have named dimnames. Had this been thought of when S was designed, then maybe vectors and 1d arrays had been the same, but when it happened, it was not an option to modify the basic vector structures. drop() is documented to drop extents of length one, of which there are none in the 1d-array case, and so it is taken as a no-op. A slightly different design would be to drop in a strict array sense, i.e. dim=c(1,1,12) would drop to dim=12, not to a vector of length 12, and THEN if the result is 1d, convert to vector (this could even be made optional, with some reason). I don't think it's at the top of anyone's agenda, though. Meanwhile, c(x) should get you there soon enough. -pd> > Best regards, > > Arni > > > > On Wed, 8 Sep 2010, Simon Urbanek wrote: > >> wrong address - did you mean R-devel? >> Simon >> >> >> >> On Sep 6, 2010, at 8:35 AM, Arni Magnusson wrote: >> >>> Bug or not, I was surprised by this behavior: >>> >>> x <- tapply(chickwts$weight, chickwts$feed, median) >>> x # array ... I'd like to convert to vector with named elements >>> drop(x) # what, still an array? >>> drop(as.matrix(x)) # this works >>> drop(t(x)) # this works >>> >>> I was expecting drop(x) to return a vector, and I suspect many R users >>> would too. The title in help(drop), "Drop Redundant Extent >>> Information", suggests that such a simple array would be converted to a >>> vector. >>> >>> Reading through the help page, I note that this is perhaps not a clear >>> bug, but somewhat unclear behavior. >>> >>> The most compact way to break the vector out of its eggshell seems to >>> be >>> >>> t(x)[,] >>> >>> but drop(x) would be much easier to read and write. There's nothing >>> particularly matrix about x, so it's not obvious that the conversion >>> should involve as.matrix(x). >>> >>> Thanks, >>> >>> Arni >> > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- Peter Dalgaard Center for Statistics, Copenhagen Business School Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
> -----Original Message----- > From: r-devel-bounces at r-project.org > [mailto:r-devel-bounces at r-project.org] On Behalf Of Arni Magnusson > Sent: Wednesday, September 08, 2010 11:55 AM > To: r-devel at r-project.org > Cc: Simon Urbanek > Subject: [Rd] Drop single-dimensional array > > Hi Simon, thank you for the concise reply. > > Do you mean the reported behavior of drop() is not a bug? > > It looks like a borderline bug to me (see below), but I'm not > the judge of > that. If this is the intended behavior and serves an actual > purpose, then > that could be explicitly documented in a \note{} on the help page. > > Such a note would slightly reduce the surprise of users > running into this > behavior. > > This is related to the oddity that one-dimensional arrays are: > > array(month.abb, dim=c(1,1,12)) # array > array(month.abb, dim=c(1,12)) # matrix > array(month.abb, dim=12) # array > > Firstly, one would expect the pattern to be > array-matrix-vector.I would expect the array() constructor function to return something of class array. (I guess matrix is acceptable because it inherits from array). One dimensional arrays and vectors act very similarly when used in R code. E.g., names(array1d) gives you the same thing as dimnames(array1d)[[1]]. Accessing them from C code probably shows more differences. Are there features beyond drop's behavior that are causing you to be concerned about differences between the 2 types? drop(x) and x[..., drop=TRUE] have always bugged me because you couldn't control which dimensions got dropped. E.g., if you have a n by 2 by 3 array and want to get the 3rd n by 2 slice of it as a matrix you can do array[,,3,drop=TRUE] except when n==1. It would be nice to be able to say "drop the 3rd dimension, throwing an error if that dimension is not 1". Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> Secondly, > it's easy to drop() the three-dimensional and two-dimensional > objects, but > drop() does nothing to the one-dimensional array. Instead, it > takes an > unintuitive combination of methods to convert a > single-dimensional to a > vector, while retaining its names. Or I may well be missing something > obvious. > > Best regards, > > Arni > > > > On Wed, 8 Sep 2010, Simon Urbanek wrote: > > > wrong address - did you mean R-devel? > > Simon > > > > > > > > On Sep 6, 2010, at 8:35 AM, Arni Magnusson wrote: > > > >> Bug or not, I was surprised by this behavior: > >> > >> x <- tapply(chickwts$weight, chickwts$feed, median) > >> x # array ... I'd like to convert to vector with named elements > >> drop(x) # what, still an array? > >> drop(as.matrix(x)) # this works > >> drop(t(x)) # this works > >> > >> I was expecting drop(x) to return a vector, and I suspect > many R users > >> would too. The title in help(drop), "Drop Redundant Extent > >> Information", suggests that such a simple array would be > converted to a > >> vector. > >> > >> Reading through the help page, I note that this is perhaps > not a clear > >> bug, but somewhat unclear behavior. > >> > >> The most compact way to break the vector out of its > eggshell seems to > >> be > >> > >> t(x)[,] > >> > >> but drop(x) would be much easier to read and write. > There's nothing > >> particularly matrix about x, so it's not obvious that the > conversion > >> should involve as.matrix(x). > >> > >> Thanks, > >> > >> Arni > > > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >