[R] two questions for R beginners

(Ted Harding) Ted.Harding at manchester.ac.uk
Mon Mar 1 15:50:57 CET 2010


On 01-Mar-10 13:57:08, Petr PIKAL wrote:
> Hi
> r-help-bounces at r-project.org napsal dne 01.03.2010 13:03:24:
> < snip>
>> > I understand that 2 dimensional rectangular matrix looks quite
>> > similar to data frame however it is only a vector with dimensions.
>> > As such it can have items of only one type (numeric, character,
>> > ...).
>> > And you can easily change dimensions of matrix.
>> > 
>> > matrix<-1:12
>> > dim(matrix) <- c(2,6)
>> > matrix
>> > dim(matrix) <- c(2,2,3)
>> > matrix
>> > dim(matrix) <-NULL
>> > matrix
>> > 
>> > So rectangular structure of printed matrix is a kind of coincidence
>> > only, whereas rectangular structure of data frame is its main
>> > feature.
>> > 
>> > Regards
>> > Petr
>> >> 
>> >> -- 
>> >> Karl Ove Hufthammer
>> 
>> Petr, I think that could be confusing! The way I see it is that
>> a matrix is a special case of an array, whose "dimension" attribute
>> is of length 2 (number of "rows", number of "columns"); and "row"
>> and "column" refer to the rectangular display which you see when
>> R prints to matrix. And this, of course, derives directly from
>> the historic rectangular view of a matrix when written down.
>> 
>> When you went from "dim(matrix)<-c(2,6)" to "dim(matrix)<-c(2,2,3)"
>> you stripped it of its special title of "matrix" and cast it out
>> into the motley mob of arrays (some of whom are matrices, but
>> "matrix" no longer is).
>> 
>> So the "rectangular structure of printed matrix" is not a coincidence,
>> but is its main feature!
> 
> Ok. Point taken. However I feel that possibility to manipulate 
> matrix/array dimensions by simple changing them as I  showed above 
> together with perceiving matrix as a **vector with dimensions**
> prevented me especially in early days from using matrices instead
> of data frames and vice versa. 
> 
> Consider cbind and rbind confusing results for vectors with unequal
> mode. 
> Far too often we can see something like that
> 
>> cbind(1:2,letters[1:2])
>      [,1] [,2]
> [1,] "1"  "a" 
> [2,] "2"  "b" 
> 
> instead of
> 
>> "
>   X1.2 letters.1.2.
> 1    1            a
> 2    2            b
> 
> and then a question why does not the result behave as expected.
> Each type of object has some features which is good for some
> type of manipulation/analysis/plotting but quite detrimental
> for others.
> 
> Regards
> Petr
> 
>> [the rest from my previous reply stripped

Well, it depends what one means by "as expected"!

One of the things about R which many (and that certainly includes
me) have to find out the hard way is that you have to *learn*
what to expect! You can't just import it from prior experience in
other contexts. So, by the time you have learned that a matrix
is such that all its elements must have the same type, whereas
the components of a list (or as special case the columns of a
dataframe) can be of different types, you expect the first result
(your "cbind(1:2,letters[1:2])"): R can coerce the numerical
elements to character type, but letters have too much character
to allow themselves be coerced into numerical.

What can be confusing is that the on-screem output of your
"data.frame(1:2,letters[1:2])" does not exhibit the "" quotes
which identify character type, so that what appears on screen
does not inform the user of what is going on. In particular,
one can not learn from the display that the 1 and 2 are numbers
and not characters. Since a and b are displayed without "", and
so are the 1 and 2, the 1 and 2 could be either -- until you
check it out by other means.

I think it is not that one should object to the behaviours of
the different types of objects. What really matters is that
one needs to learn what they are and why they behave as they do,
and not be misled by appearances.

And this includes the somtimes unpredictable behaviours of the
print methods.

In the context of this thread, I think the issues that have
been raised by the current discussion on matrices and dataframes
should receive sympathetic attention in Patrick Burns' project.

To close:

  cbind(c(pi,pi),letters[1:2])
  #      [,1]               [,2]
  # [1,] "3.14159265358979" "a" 
  # [2,] "3.14159265358979" "b" 
  data.frame(c(pi,pi),letters[1:2])
  #   c.pi..pi. letters.1.2.
  # 1  3.141593            a
  # 2  3.141593            b
  pi
  # [1] 3.141593
  as.character(pi)
  # [1] "3.14159265358979"

That raises a few questions about "expectations" too!
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 01-Mar-10                                       Time: 14:50:50
------------------------------ XFMail ------------------------------



More information about the R-help mailing list