Mike Miller
2014-Dec-25 22:45 UTC
[R] ave(x, y, FUN=length) produces character output when x is character
On Thu, 25 Dec 2014, Mike Miller wrote:> I was going to ask a question about it how to test that an object is a > vector, but then I found this: > > "is.vector() does not test if an object is a vector. Instead it returns > TRUE only if the object is a vector with no attributes apart from names. > Use is.atomic(x) || is.list(x) to test if an object is actually a > vector." > > From here: > > http://adv-r.had.co.nz/Data-structures.html#vectorsBut here... https://stat.ethz.ch/R-manual/R-devel/library/base/html/vector.html ...I read, "Note that factors are *not* vectors" (emphasis theirs), yet...> d <- gl(2,2)> is.factor(d)[1] TRUE> is.atomic(d) || is.list(d)[1] TRUE> is.list(d)[1] FALSE> is.atomic(d)[1] TRUE> is.vector(d)[1] FALSE So the factor is not a vector according to R documentation, but it is a vector according to the Wickham test, and it is not a vector according to is.vector(). Admittedly, the latter seems not to mean much to the R experts. Maybe a factor is just a vector with additional attributes. Mike
Bert Gunter
2014-Dec-25 23:19 UTC
[R] ave(x, y, FUN=length) produces character output when x is character
You persist in failing to read the docs! Moreover, neither Hadley Wickham, nor anyone else, is the authoritative source for R usage (other than for the (many!) packages he, himself has authored). R's Help pages and manuals -- and ultimately the source code -- are the only such source. ?factor says in its very first line: "The function factor is used to encode a **vector** as a factor (the terms ?category? and ?enumerated type? are also used for factors)...." (emphasis added) and:> f <- factor (letters[1:3]) > f[1] a b c Levels: a b c> attributes(f)$levels [1] "a" "b" "c" $class [1] "factor"> is.vector(f)[1] FALSE> attributes(f) <- NULL> f[1] 1 2 3> is.vector(f)[1] TRUE Don't you think it's time to call a halt to this? Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." Clifford Stoll On Thu, Dec 25, 2014 at 2:45 PM, Mike Miller <mbmiller+l at gmail.com> wrote:> On Thu, 25 Dec 2014, Mike Miller wrote: > >> I was going to ask a question about it how to test that an object is a >> vector, but then I found this: >> >> "is.vector() does not test if an object is a vector. Instead it returns >> TRUE only if the object is a vector with no attributes apart from names. Use >> is.atomic(x) || is.list(x) to test if an object is actually a vector." >> >> From here: >> >> http://adv-r.had.co.nz/Data-structures.html#vectors > > > > But here... > > https://stat.ethz.ch/R-manual/R-devel/library/base/html/vector.html > > ...I read, "Note that factors are *not* vectors" (emphasis theirs), yet... > > >> d <- gl(2,2) > > >> is.factor(d) > > [1] TRUE > >> is.atomic(d) || is.list(d) > > [1] TRUE > >> is.list(d) > > [1] FALSE > >> is.atomic(d) > > [1] TRUE > >> is.vector(d) > > [1] FALSE > > So the factor is not a vector according to R documentation, but it is a > vector according to the Wickham test, and it is not a vector according to > is.vector(). Admittedly, the latter seems not to mean much to the R > experts. Maybe a factor is just a vector with additional attributes. > > Mike
Mike Miller
2014-Dec-26 04:30 UTC
[R] ave(x, y, FUN=length) produces character output when x is character
On Thu, 25 Dec 2014, Bert Gunter wrote:> You persist in failing to read the docs!"the docs" -- do those exclude those I have been quoting and linking to?> Moreover, neither Hadley Wickham, nor anyone else, is the authoritative > source for R usage (other than for the (many!) packages he, himself has > authored). R's Help pages and manuals -- and ultimately the source code > -- are the only such source.Very pendantic. Are you saying that Hadley Wickham's claim was incorrect? To repeat, he said that this would return TRUE if x were a vector: is.atomic(x) || is.list(x) If you think that is wrong, I'd be interested to know more about that.> ?factor says in its very first line: > > "The function factor is used to encode a **vector** as a factor (the > terms ?category? and ?enumerated type? are also used for factors)...." > (emphasis added)So what? Are you saying that a factor *is* a vector? I quoted this before, but I'll repeat it here -- from the third paragaraph of the Details section of ?vector: Note that factors are _not_ vectors; ?is.vector? returns ?FALSE? and ?as.vector? converts a factor to a character vector for ?mode = "any"?. I guess that is an "authoritative source" by your criteria even though it isn't in the first line of the page.>> f <- factor (letters[1:3]) >> f > [1] a b c > Levels: a b c > >> attributes(f) > $levels > [1] "a" "b" "c" > > $class > [1] "factor" > >> is.vector(f) > [1] FALSE > >> attributes(f) <- NULL > >> f > [1] 1 2 3 >> is.vector(f) > [1] TRUEAnd your point is what? Yes, we can convert between different kinds of objects. Are you saying that a factor *is* a vector because you can coerce it into a vector by removing its attributes? I do think it is very central to this discussion that attributes(x) <- NULL makes x into a vector, and that is not true just for factors, but also matrices, as you showed me earlier. Following your lead, this is another example:> b <- 1:4> attr(b, "dim") <- c(2,2)> is.matrix(b)[1] TRUE Does that mean that "a matrix is a vector"? Not for me, but it does make it easy to see how that concept helps people to understand the internal workings of R. Gabor Grothendieck wrote, "I think its the idea that in R all data objects are vectors (for some notion of vector) in the sense that all Lisp objects are lists, all APL objects are arrays and all tcl objects are character strings." That's how I've been thinking about it, too, but I'm not sure that *all* data objects are vectors in this sense. If that were the case, the Wickham test would always return TRUE.> Don't you think it's time to call a halt to this?You go first. Mike -- Michael B. Miller, Ph.D. University of Minnesota http://scholar.google.com/citations?user=EV_phq4AAAAJ