Article View: aus.stats.s
Article #182Answer to Teaser
From: allan
Date: Wed, 27 Jan 1993 23:51
Date: Wed, 27 Jan 1993 23:51
89 lines
3972 bytes
3972 bytes
Last week I sent out a teaser regarding subscripting. I received replies
from the following people:
trevor@research.att.com (Trevor Hastie)
cberry@tajo.UCSD.EDU (Charles C. Berry)
pat@statsci.com (Pat Burns)
john@marc.cri.nz (John Maindonald)
bill@statsci.com (Bill Dunlap)
molinari@cumuli.ethz.ch (Luciano Molinari)
tim@fnmcps.fandm.edu (Tim Hesterberg)
rolf@math.unb.ca (Rolf Turner)
liming@next.neuro.emory.edu (Liming Shen)
edland@u.washington.edu (Steve Edland)
shawn@aragorn.ori.org (Shawn Boles)
cwmnol@rivm.nl (Arnold Dekkers)
andreas@iso.wwz.unibas.ch (Andreas Krause)
wfszewc@afterlife.ncsc.mil (William F. Szewczyk)
Kudos to Charles, Pat (actually Peter Schumacher, according to Pat), John,
Rolf, Andreas and William who all more or less came up with the proper
explanation of the apparently bizzare answers. It wasn't clear from the
responses who had guessed the correct answers before going to the terminal.
The puzzle:
Suppose that x <- 1:2. Without actually typing anything at
your computer, figure out the values of:
(a) x["a"]
(b) x[x="a"]
(c) x[y="a"]
Now type them in and explain any discrepancies between what
you guessed and what you saw.
The answer (easy if you have S or Splus):
> x["a"]
a
NA
> x[x="a"]
[1] "a" ""
> x[y="a"]
a
NA
The explanation:
1. The first answer follows from two rules: first, non-character
atomic vectors are conceptually extended infinitely with NAs beyond
their end and character vectors are conceptually extended with null
strings; and second, unmatched names in a character subscript index
outside the range of the vector.
2. The second is more subtle. To understand it, look at the definition
of the subscript function, "[":
> get("[")
function(x, ..., drop = T)
.Internal(x[..., drop = drop], "S_extract", T, 1)
The important thing here is that "[" takes two named arguments ("x" and
"drop") and arbitrarily many other arguments ("..."), which will be
used to compute the desired subset. In a sense, the notation y[1] is
just syntactic sugar, and if you were to write this out in a canonical
form, it would be "["(y,1) (this works -- try it!). In our case, the
canonical form would be "["(x,x="a"). Recall that named arguments are
matched first. Hence, the second argument in the call is matched to
the first formal argument of "[" (because both are named "x") and
therefore the remaining argument is matched to the ...; it is as if you
said "a"[x] -- try it! The second element of x subscripts outside the
range of "a", which explains the "" in the answer.
3. The canonical form in this case is "["(x,y="a"), and now the first
argument of the call gets matched positionally against the formal argument
named "x", leaving the "a" to match the "...", and therefore giving
something that is exactly equivalent to the first problem, x["a"].
There are lots more variations on this theme, a number of which I received
from the above people. But enough is enough. It's hard to see how an
expression like x[x="a"] could be useful, but it does serve to point out
some of the finer points of subscripting and argument matching.
o========================================================o
| Allan R. Wilks allan@research.att.com |
| AT&T Bell Labs, Room 2C-283 office: (908)582.4550 |
| 600 Mountain Avenue fax: (908)582.3340 |
| Murray Hill, NJ 07974 |
o========================================================o
Technical Details
Message-ID:
<9301272351.AA19005@dmssyd.syd.dms.CSIRO.AU>Path:
arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!metro!dmssyd.syd.dms.CSIRO.AU!newsShow Full Headers
Complete Article Headers
Newsgroups: aus.stats.s Path: arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!metro!dmssyd.syd.dms.CSIRO.AU!news From: allan@research.att.com Subject: Answer to Teaser Message-ID: <9301272351.AA19005@dmssyd.syd.dms.CSIRO.AU> X-Gateway: dmssyd.syd.dms.CSIRO.AU Lines: 89 Sender: daemon@syd.dms.CSIRO.AU Organization: CSIRO Div. Maths & Stats, CSIRO Div. Applied Physics Date: Wed, 27 Jan 1993 23:51:39 GMT