Hi all, I just discovered that R considers characters to be really big:> "a" > 999[1] TRUE> "a" > 9e307[1] TRUE> "a" > 9e308[1] FALSE and that some characters are literally infinitely big:> "Z" >= Inf[1] TRUE although not all:> "a" > Inf[1] FALSE This came as a surprise to me (although it is quite possibly a trivial issue), and I'd appreciate any information about why R considers character vectors to even be comparable to numeric vectors, and why it considers characters to have very large values. Thanks! Ista
Dear Ista, This is a consequence of coercion of the numbers to character:> c("Z", "a", 999, Inf)[1] "Z" "a" "999" "Inf"> sort(c("Z", "a", 999, Inf))[1] "999" "a" "Inf" "Z" I hope this helps, John On Tue, 3 Jan 2012 17:56:29 -0500 Ista Zahn <istazahn at gmail.com> wrote:> Hi all, > > I just discovered that R considers characters to be really big: > > > "a" > 999 > [1] TRUE > > "a" > 9e307 > [1] TRUE > > "a" > 9e308 > [1] FALSE > > and that some characters are literally infinitely big: > > > "Z" >= Inf > [1] TRUE > > although not all: > > > "a" > Inf > [1] FALSE > > > This came as a surprise to me (although it is quite possibly a trivial > issue), and I'd appreciate any information about why R considers > character vectors to even be comparable to numeric vectors, and why it > considers characters to have very large values. > > Thanks! > > Ista > > ______________________________________________ > R-help at r-project.org mailing list > 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.
There's a coercion to character implicit (i.e., the number 999 gets converted to the string "999") and then comparison is done in lexical (dictionary) order in which digits are lower than characters. You'll also note you get apparently strange behavior like "34" < "9" if you don't think about things in terms of dictionary orderings. Does that make sense? Michael On Tue, Jan 3, 2012 at 4:56 PM, Ista Zahn <istazahn at gmail.com> wrote:> Hi all, > > I just discovered that R considers characters to be really big: > >> "a" > 999 > [1] TRUE >> "a" > 9e307 > [1] TRUE >> "a" > 9e308 > [1] FALSE > > and that some characters are literally infinitely big: > >> "Z" >= Inf > [1] TRUE > > although not all: > >> "a" > Inf > [1] FALSE > > > This came as a surprise to me (although it is quite possibly a trivial > issue), and I'd appreciate any information about why R considers > character vectors to even be comparable to numeric vectors, and why it > considers characters to have very large values. > > Thanks! > > Ista > > ______________________________________________ > R-help at r-project.org mailing list > 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.