Apply dimnames (PR#385)
J.C.Rougier@durham.ac.uk
J.C.Rougier@durham.ac.uk
Wed, 22 Dec 1999 12:22:35 +0100 (MET)
This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.
Send mail to mime@docserver.cac.washington.edu for more info.
---559023410-1251336619-945861747=:5357
Content-Type: TEXT/PLAIN; charset=US-ASCII
While fiddling with apply I noticed that there appears to be an error in
the dimnames, which actually show in the example.
x <- cbind(x1 = 3, x2 = c(4:1, 2:5))
dimnames(x)[[1]] <- letters[1:8]
First:
apply(x, 2, sort)
I don't think the row names should not be retained here as they are not
common to all columns.
Second:
cave <- function(x, c1,c2) c(mean(x[c1]),mean(x[c2]))
apply(x,1, cave, c1="x1", c2=c("x1","x2"))
I don't think the row names should be c("x1", "x2") which are left over
from the object x, but should be determined by the function cave, even to
the extent of not being present at all (the fact that cave returns a
vector of length 2 is just a coincidence).
I've attached a new apply (Apply) which takes care of these two problems,
at the cost of an extra lapply, anticipating an internal lapply shortly to
speed everything up. Basically if FUN returns a vector and that vector
has common names across the columns (or, more generally, across MARGIN)
then these names are used to label the first extent; otherwise no names
are used.
Cheers, Jonathan.
Jonathan Rougier Science Laboratories
Department of Mathematical Sciences South Road
University of Durham Durham DH1 3LE
"[B]egin upon the precept ... that the things we see are to be
weighed in the scale with what we know" (Meredith, 1879, The Egoist)
---559023410-1251336619-945861747=:5357
Content-Type: TEXT/PLAIN; charset=US-ASCII; name="apply.R"
Content-Transfer-Encoding: BASE64
Content-ID: <Pine.GSO.4.20.9912221122270.5357@laplace>
Content-Description:
Content-Disposition: attachment; filename="apply.R"
IkFwcGx5IiA8LQ0KZnVuY3Rpb24oeCwgTUFSR0lOLCBGVU4sIC4uLikNCnsN
CiAgICBGVU4gPC0gbWF0Y2guZnVuKEZVTikNCiAgICBkeCA8LSBkaW0oeCkN
CiAgICBpZiAoaXMubnVsbChkeCkpDQogICAgICBzdG9wKCJcInhcIiBtdXN0
IGJlIGEgbWF0cml4IG9yI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y5hcnJheSh2KSk9PWRtMSkpKSkgew0KDQogICAgICAgICMgY2Vy
dGFpbiB0byByZXR1cm4gaW4gdGhpcyBibG9jaw0KDQogICAgICAgIGlmIChs
ZW5ndGgoZG0xKT09MSkgew0KDQogICAgICAgICAgaWYgKGRtMT09MSkNCiAg
ICAgICAgICAgIHJldHVybihhcnJheSh1bmxpc3QoeGxsKSwgZHhbTUFSR0lO
XSwgaWYgKGdvdC5uYW1lcykNCiAgICAgICAgICAgICAgZG54W01BUkdJTl0g
ZWxzZSBOVUxMKSkNCiAgICAgICAgICBlbHNlIHsNCiAgICAgICAgICAgIGRu
MSA8LSBuYW1lcyh4bGxbWzFdXSkNCiAgICAgICAgICAgIGlmICghaXMubnVs
bChkbjEpICYmIGFsbChzYXBwbHkoeGxsLA0KICAgICAgICAgICAgICBmdW5j
dGlvbih2KSB7DQogICAgICAgICAgICAgICBkbiA8LSBuYW1lcyh2KQ0KICAg
ICAgICAgICAgICAgIWlzLm51bGwoZG4pICYmIGFsbChkbj09ZG4xKQ0KICAg
ICAgICAgICAgICB9KSkpDQogICAgICAgICAgICAgICAgcmV0dXJuKGFycmF5
KHVubGlzdCh4bGwpLCBjKGRtMSwgZHhbTUFSR0lOXSksIGMobGlzdChkbjEp
LA0KICAgICAgICAgICAgICAgICAgaWYgKGdvdC5uYW1lcykgZG54W01BUkdJ
Tl0gZWxzZSByZXAobGlzdChOVUxMKSwNCiAgICAgICAgICAgICAgICAgIGxl
bmd0aChNQVJHSU4pKSkpKQ0KICAgICAgICAgIH0NCiAgICAgICAgfQ0KDQog
ICAgICAgIHJldHVybihhcnJheSh1bmxpc3QoeGxsKSwgYyhwcm9kKGRtMSks
IGR4W01BUkdJTl0pLCBpZg0KICAgICAgICAgIChnb3QubmFtZXMpIGMobGlz
dChOVUxMKSwgZG54W01BUkdJTl0pIGVsc2UgTlVMTCkpDQoNCiAgICAgIH0N
CiAgICB9DQoNCiAgICBpZiAoZ290Lm5hbWVzICYmIGxlbmd0aChNQVJHSU4p
PT0xKQ0KICAgICAgbmFtZXMoeGxsKSA8LSBkbnhbW01BUkdJTl1dDQogICAg
eGxsDQp9DQo=
---559023410-1251336619-945861747=:5357--
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._