Jeff Newmiller
2014-Dec-25 08:59 UTC
[R] ave(x, y, FUN=length) produces character output when x is character
You have written a lot, Mike, as though we did not know it. You are not the
only one with math and multiple computing languages under your belt. The point
Bert made is that the concept that a matrix IS-A vector is not just an
implementation detail in R... it helps the practitioner keep straight why things
like a[3] is perfectly valid when a is a matrix, and why
a*a
[,1] [,2]
[1,] 1 9
[2,] 4 16
is true. I understand why you are uncomfortable with it, as I was once, but this
is how R works so you are only impeding your own effectiveness by clinging to
theory on this point.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live
Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On December 24, 2014 11:15:28 PM PST, Mike Miller <mbmiller+l at
gmail.com> wrote:>On Wed, 24 Dec 2014, Bert Gunter wrote:
>
>> You are again misinterpreting because you have not read the docs,
>> although this time I will grant that they are to some extent
>misleading.
>>
>> First of all, a matrix _IS_ a vector:
>>
>>> a <- matrix(1:4, 2,2)
>>> a[3] ## vector indexing works because it is a vector
>> [1] 3
>>
>> In fact, a matrix (or array) is a vector with a "dim"
attribute. This
>> is documented in ?matrix:
>>
>> "is.matrix returns TRUE if x is a vector and has a "dim"
attribute of
>> length 2) and FALSE otherwise."
>
>But a vector has no such attribute, so a matrix is not a vector which
>is
>why you see this:
>
>> a <- matrix(1:4, 2,2)
>> is.vector(a)
>[1] FALSE
>
>Of course the matrix can be coerced back into a vector just as the
>vector
>was coerced into a matrix:
>
>> b <- as.vector(a)
>> is.vector(b)
>[1] TRUE
>
>
>> Your confusion arises because, despite its name, is.vector() does not
>
>> actually test whether something "is" a vector (after all
these are
>all
>> abstractions; what it "is" is contents of memory, implemented
as a
>> linked list or some such). ?is.vector tells you:
>>
>> "is.vector returns TRUE if x is a vector of the specified mode
having
>> no attributes other than names. It returns FALSE otherwise."
>
>So that means that a vector in R has no attributes other than names.
>
>
>> An array has a "dim" attribute, so is.vector() returns FALSE
on it.
>But
>> it actually _is_ ("behaves like") a vector (in column major
>> order,actually).
>
>An array is a vector with additional attributes which cause it to be an
>
>array rather than a vector. This is why R says FALSE when we query it
>about an array using is.vector().
>
>
>> Now you may complain that this is confusing and I would agree. Why is
>it
>> this way? I dunno -- probably due to historical quirks -- evolution
>is
>> not necessarily orderly. But that's the way it is; that's the
way
>it's
>> documented; and tutorials will tell you about this (that's how I
>> learned). So please stop guessing and intuiting and read the docs to
>> understand how things work.
>
>I don't think it is confusing. This is the kind of behavior I'm
used
>to
>from other programs like Octave/MATLAB. A vector is just an ordered
>list
>of numbers. Those numbers can be put into matrices or
>higher-dimensional
>arrays, but they then become something more than just a vector. A
>vector
>like 1:4 becomes a 2x2 matrix when we do matrix(1:4, 2,2) such that the
>
>number 3 which was just the third element before (and still is) is now
>also the [1,2] element of a matrix. It didn't have that before, back
>when
>it was a vector, but now that it has become something more than a
>vector,
>it has that new property. We can take that away using as.vector().
>
>In many situations the behavior of the R vector and the same values in
>a
>matrix format will be very different:
>
>> a <- 1:4
>> b <- matrix(a, 2,2)
>> a %*% a
> [,1]
>[1,] 30
>> b %*% b
> [,1] [,2]
>[1,] 7 15
>[2,] 10 22
>> b %*% t(b)
> [,1] [,2]
>[1,] 10 14
>[2,] 14 20
>> a %*% t(a)
> [,1] [,2] [,3] [,4]
>[1,] 1 2 3 4
>[2,] 2 4 6 8
>[3,] 3 6 9 12
>[4,] 4 8 12 16
>
>That is not true in ave(), as I showed earlier, because it uses the
>vector
>ordering of elements in the x matrix or array (what one would get from
>as.vector()) to form the correspondence with the factor.
>
>I get your idea, but I don't think it is correct to say "a matrix
is a
>vector." Rather, I would say that there is a standard way in which one
>
>can create a one-to-one correspondence between the elements of a matrix
>of
>given dimensions and the elements of a vector. I believe this is
>usually
>called "fortran indexing," or at least that is what it is called
in
>Octave. The same thing is done with vectorization and the vec()
>operator
>in mathematics:
>
>http://en.wikipedia.org/wiki/Vectorization_(mathematics)
>
>But in math as in computing, we wouldn't say that a matrix *is* a
>vector.
>If vec(A) = v, that does not mean that A = v. In R, it looks like
>as.vector() can do what vec() does, and more.
>
>Mike
Mike Miller
2014-Dec-25 09:23 UTC
[R] ave(x, y, FUN=length) produces character output when x is character
On Thu, 25 Dec 2014, Jeff Newmiller wrote:> You have written a lot, Mike, as though we did not know it. You are not > the only one with math and multiple computing languages under your belt.I'm not assuming that you and Bert don't know things, but I do expect to have a wider audience -- when I search for things, sometimes I find my postings from 10 years ago. I'm probably not the only one.> The point Bert made is that the concept that a matrix IS-A vector is not > just an implementation detail in R... it helps the practitioner keep > straight why things like a[3] is perfectly valid when a is a matrix, and > why > > a*a > [,1] [,2] > [1,] 1 9 > [2,] 4 16 > > is true. I understand why you are uncomfortable with it, as I was once, > but this is how R works so you are only impeding your own effectiveness > by clinging to theory on this point.Sorry, your concepts aren't helping me and I'm not uncomfortable with what R is doing. In fact, what I wrote earlier is that R seems to be much like Octave/MATLAB, which I have used even more than I've used R. The use of what Octave calls "fortran indexing" is an example. We can refer to matrix element a[3] in R or element a(3) in Octave and we're referring to the same element. It's also the third element of vec(a). That doesn't mean that 'a' is a vector. R says that it is not a vector. That doesn't confuse me. I can refer to the third element of a matrix even if the matrix is not a vector. I don't understand how your concept helps in understanding a*a. It's an element-by-element product (also called Hadamard product). In Octave it would be a.*a. The matrix product in R is a%*%a and in Octave it is a*a. I find nothing confusing about any of this and I don't see how conceiving of 'a' as a vector helps at all. The difference between our concepts is that this makes no sense within your framework where you think of a matrix as being a vector:> a <- matrix(1:4, 2,2) > is.vector(a)[1] FALSE But to me it makes perfect sense because 'a' is a matrix and a matrix is not a vector. We might say that it contains a vector, or that the elements of the matrix are constructed from the elements of a vector, but not that the matrix is a vector. It is a different class of object:> class(a)[1] "matrix" Mike
Mike Miller
2014-Dec-25 09:28 UTC
[R] ave(x, y, FUN=length) produces character output when x is character
On Thu, 25 Dec 2014, Mike Miller wrote:> On Thu, 25 Dec 2014, Jeff Newmiller wrote: > >> You have written a lot, Mike, as though we did not know it. You are >> not the only one with math and multiple computing languages under your >> belt. > > I'm not assuming that you and Bert don't know things, but I do expect to > have a wider audience -- when I search for things, sometimes I find my > postings from 10 years ago. I'm probably not the only one.To clarify one point: I write some things to organize my own ideas. I also think I'm not the only one who will want to see it. I don't believe that I know more about R or programming than you guys do. I consider that an extremely remote possibility, at best. That doesn't mean you are right about everything, or about any particular thing. Mike