Semantics of sequences in R
On Sun, Feb 22, 2009 at 4:12 PM, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
I think this was posted to the wrong list, so my followup is going to R-devel.
OK.
On 22/02/2009 3:42 PM, Stavros Macrakis wrote:
Inspired by the exchange between Rolf Turner and Wacek Kusnierczyk, I
thought I'd clear up for myself the exact relationship among the
various sequence concepts in R, including not only generic vectors
(lists) and atomic vectors, but also pairlists, factor sequences,
date/time sequences, and difftime sequences.
I tabulated type of sequence vs. property to see if I could make sense
of all this. The properties I looked at were the predicates
is.{vector,list,pairlist}; whether various sequence operations (c,
rev, unique, sort, rle) can be used on objects of the various types,
and if relevant, whether they preserve the type of the input; and what
the length of class( as.XXX (1:2) ) is.
Here are the results (code to reproduce at end of email):
numer list plist fact POSIXct difft
is.vector TRUE TRUE FALSE FALSE FALSE FALSE
is.list FALSE TRUE TRUE FALSE FALSE FALSE
is.pairlist FALSE FALSE TRUE FALSE FALSE FALSE
c_keep? TRUE TRUE FALSE FALSE TRUE FALSE
rev_keep? TRUE TRUE FALSE TRUE TRUE TRUE
unique_keep? TRUE TRUE "Err" TRUE TRUE FALSE
sort_keep? TRUE "Err" "Err" TRUE TRUE TRUE
rle_len 2 "Err" "Err" "Err" "Err" "Err"
Alas, this tabulation, rather than clarifying things for me, just
confused me more -- the diverse treatment of sequences by various
operations is all rather bewildering.
But you are asking lots of different questions, so of course you should get different answers. For example, the first three rows are behaving exactly as documented.
Yes, I wasn't questioning that. This started out as an exploration of Rolf's claim that "vectors can be considered to be lists", which I think the table shows pretty clearly not to be true. He did qualify the claim with "At a certain level.", but I don't know what that level is....
(Perhaps the functions should have been designed differently, but a pretty-looking matrix isn't an argument for that. Give some examples of how the documented behaviour is causing problems.)
From my own experience, and the experience of colleagues who have
tried to learn R, I can tell you that these idiosyncracies make learning the system more difficult. A "pretty-looking matrix" is a reflection of an orthogonal design, which is generally considered to be a good thing. Many of the missing operations are perfectly meaningful and useful.
...But it may make more sense to completely hide pairlists,
I agree that the pairlist cases are the least interesting.
(BTW, your description of your last row doesn't match what you did, as far as I can see.)
Yes, sorry, older draft....
Wouldn't it be easier to teach, learn, and use R if there were more consistency in the treatment of sequences?
Which ones in particular should change? What should they change to? What will break when you do that?
In many cases, the orthogonal design is pretty straightforward. And in the cases where the operation is currently an error (e.g. sort(list(...))), I'd hope that wouldn't break existing code. There are certainly cases which would be hard to change without breaking existing code....
Generally R core members are reluctant to take on work just because someone else thinks it would be nice if they did.
I understand this principle quite well, having been a contributor to other similar projects. I was simply starting the discussion. After all, if the core group disagrees that the functions should be made more orthogonal, it is a waste of my time to submit code.
If you want to do this, that's one thing,
I have already suggested code changes in some (pretty trivial) cases
-- see r-help Feb 6, 2009 6:17 PM "Operations on difftime (abs, /, c)"
-- but perhaps r-help was the wrong place to send them. I will
forward to r-devel. And I will be happy to work on some of the
consistency issues I've mentioned here.
-s