rasche@molgen.mpg.de
2005-Dec-13 15:05 UTC
[Rd] 0/1 vector for indexing leads to funny behaviour (PR#8389)
Full_Name: Axel Rasche Version: 2.2.0 OS: Linux Submission from: (NULL) (141.14.21.81) Dear Debuggers, This is not a serious problem. Are 0/1 vectors intended to be used as index vectors? If yes, there is a bug. If not, it leads just to some funny behaviour rather than an error message. In the appendix is some simple code to reproduce the problem. A logical vector as.logic(a) helps by indexing the vector b. The 0/1 vector a just returns the first value "a". But as many times as there is a 1 in a. Best regards, Axel Appendix: b = c("a","b","c","d") a = c(0,1,1,0) b[as.logical(a)] b[a] a = c(1,0,1,0) b[as.logical(a)] b[a] a = c(0,1,1,1) b[as.logical(a)] b[a]
Peter Dalgaard
2005-Dec-13 16:37 UTC
[Rd] 0/1 vector for indexing leads to funny behaviour (PR#8389)
rasche at molgen.mpg.de writes:> Full_Name: Axel Rasche > Version: 2.2.0 > OS: Linux > Submission from: (NULL) (141.14.21.81) > > > Dear Debuggers, > > This is not a serious problem. Are 0/1 vectors intended to be used as index > vectors? If yes, there is a bug. If not, it leads just to some funny behaviour > rather than an error message. > > In the appendix is some simple code to reproduce the problem. A logical vector > as.logic(a) helps by indexing the vector b. The 0/1 vector a just returns the > first value "a". But as many times as there is a 1 in a.Yes, that is completely as intended. Zeros in a numerical index vector produce nothing and ones produce the first element. The documentation could arguably be better on this point though.> Best regards, > Axel > > > Appendix: > > b = c("a","b","c","d") > a = c(0,1,1,0) > b[as.logical(a)] > b[a] > a = c(1,0,1,0) > b[as.logical(a)] > b[a] > a = c(0,1,1,1) > b[as.logical(a)] > b[a] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Tony Plate
2005-Dec-13 18:04 UTC
[Rd] 0/1 vector for indexing leads to funny behaviour (PR#8389) (maybe a documentation deficiency?)
Yes, 0/1 (numeric) are intended to be used as index vectors -- and they have the semantics of numeric indices, which is that 0 elements in the index are omitted from the result. This can be a very useful mode of operation in many situations. I was going to write "This is described in both the introduction to R, and in the documentation for '['", except that I checked before I wrote and was surprised to be unable to any discussion of zeros in indexing in any of the first three places I looked: (1) help page for '[' (There is discussion of zero indices here, but only in the context of using matrices to index matrices, not in the context of ordinary vector indices). (2) Section 2.7 "Index vectors: selecting and modifying subsets of a data set" in "An Introduction to R", which does say this about numeric indices: 2. A vector of positive integral quantities. In this case the values in the index vector must lie in the set {1, 2, . . . , length(x)} (This seems to commit the sin of not telling the whole truth.) (3) Section 5.5 "Array Indexing. Subsections of an array" (In "An Introduction to R") Question for others: did I miss something obvious, or is this a documentation deficiency that zeros in indices are not discussed in 3 of some obvious first places to look? If indeed this is a documentation deficiency, I'm happy to contribute documentation patch, but I await other opinions before spending any time on that. -- Tony Plate rasche at molgen.mpg.de wrote:> Full_Name: Axel Rasche > Version: 2.2.0 > OS: Linux > Submission from: (NULL) (141.14.21.81) > > > Dear Debuggers, > > This is not a serious problem. Are 0/1 vectors intended to be used as index > vectors? If yes, there is a bug. If not, it leads just to some funny behaviour > rather than an error message. > > In the appendix is some simple code to reproduce the problem. A logical vector > as.logic(a) helps by indexing the vector b. The 0/1 vector a just returns the > first value "a". But as many times as there is a 1 in a. > > Best regards, > Axel > > > Appendix: > > b = c("a","b","c","d") > a = c(0,1,1,0) > b[as.logical(a)] > b[a] > a = c(1,0,1,0) > b[as.logical(a)] > b[a] > a = c(0,1,1,1) > b[as.logical(a)] > b[a] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
rasche@molgen.mpg.de
2005-Dec-13 18:09 UTC
[Rd] 0/1 vector for indexing leads to funny behaviour (PR#8389)
Hi, OK, no bug. I got stuck into to much indexing. Now I understand this point definitely better. Sorry for disturbing you, Axel Tony Plate wrote:> Yes, 0/1 (numeric) are intended to be used as index vectors -- and they > have the semantics of numeric indices, which is that 0 elements in the > index are omitted from the result. This can be a very useful mode of > operation in many situations. > > I was going to write "This is described in both the introduction to R, > and in the documentation for '['", except that I checked before I wrote > and was surprised to be unable to any discussion of zeros in indexing in > any of the first three places I looked: > > (1) help page for '[' (There is discussion of zero indices here, but > only in the context of using matrices to index matrices, not in the > context of ordinary vector indices). > > (2) Section 2.7 "Index vectors: selecting and modifying subsets of a > data set" in "An Introduction to R", which does say this about numeric > indices: > 2. A vector of positive integral quantities. In > this case the values in the index vector must > lie in the set {1, 2, . . . , length(x)} > (This seems to commit the sin of not telling the whole truth.) > > (3) Section 5.5 "Array Indexing. Subsections of an array" (In "An > Introduction to R") > > Question for others: did I miss something obvious, or is this a > documentation deficiency that zeros in indices are not discussed in 3 of > some obvious first places to look? > > If indeed this is a documentation deficiency, I'm happy to contribute > documentation patch, but I await other opinions before spending any time > on that. > > -- Tony Plate > > rasche at molgen.mpg.de wrote: > >> Full_Name: Axel Rasche >> Version: 2.2.0 >> OS: Linux >> Submission from: (NULL) (141.14.21.81) >> >> >> Dear Debuggers, >> >> This is not a serious problem. Are 0/1 vectors intended to be used as >> index >> vectors? If yes, there is a bug. If not, it leads just to some funny >> behaviour >> rather than an error message. >> >> In the appendix is some simple code to reproduce the problem. A >> logical vector >> as.logic(a) helps by indexing the vector b. The 0/1 vector a just >> returns the >> first value "a". But as many times as there is a 1 in a. >> >> Best regards, >> Axel >> >> >> Appendix: >> >> b = c("a","b","c","d") >> a = c(0,1,1,0) >> b[as.logical(a)] >> b[a] >> a = c(1,0,1,0) >> b[as.logical(a)] >> b[a] >> a = c(0,1,1,1) >> b[as.logical(a)] >> b[a] >> >> ______________________________________________ >> R-devel at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel >> >-- ******************************************* Dipl. Math. ETH Axel Rasche Max-Planck-Institute for Molecular Genetics Department Lehrach (Vertebrate Genomics) Ihnestrasse 63-73 D-14195 Berlin-Dahlem GERMANY Tel. ++49-30-8413-1289 Fax ++49-30-8413-1380
ripley@stats.ox.ac.uk
2005-Dec-13 18:10 UTC
[Rd] 0/1 vector for indexing leads to funny behaviour (PR#8389)
It is peculiar behaviour on your part, but the developers do not find it at all `funny'. On Tue, 13 Dec 2005 rasche at molgen.mpg.de wrote:> Full_Name: Axel Rasche > Version: 2.2.0 > OS: Linux > Submission from: (NULL) (141.14.21.81) > > > Dear Debuggers, > > This is not a serious problem. Are 0/1 vectors intended to be used as index > vectors? If yes, there is a bug. If not, it leads just to some funny behaviour > rather than an error message.They are, and in your example they work as documented. You have not even told us what you think the bug is. What happens in clearly stated in the R Language Definition and in all good reference books on R.> In the appendix is some simple code to reproduce the problem. A logical vector > as.logic(a) helps by indexing the vector b. The 0/1 vector a just returns the > first value "a". But as many times as there is a 1 in a.As it should.> > Best regards, > Axel > > > Appendix: > > b = c("a","b","c","d") > a = c(0,1,1,0) > b[as.logical(a)] > b[a] > a = c(1,0,1,0) > b[as.logical(a)] > b[a] > a = c(0,1,1,1) > b[as.logical(a)] > b[a]-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595