Article View: aus.stats.s
Article #16170lists to arrays and row and column sums.
From: hoqas!hoqas2!david
Date: Tue, 28 Jan 1992 16:04
Date: Tue, 28 Jan 1992 16:04
33 lines
1721 bytes
1721 bytes
I use the following function to convert lists to matrices.
l2m <- function(...) do.call("cbind", ...)
If l is a list of equal length vectors, then
m <- l2m(l)
Will create a matric out of the vectors. This has the advantage that
it avoids an explicit loop over the elements of the list and also
maintains the list names as column names. To join matrix elements
into a single matrix by rows, the cbind could be replaced by rbind.
Also, be aware that using apply for row and columns sums is horrible.
Since apply has an explicit loop in its definition it is very slow.
This applies to other uses of apply as well, but for row and column
sums there is a much more efficient solution available by using matrix
multiply.
rowsum <- function(x) c(x %*% rep(1, ncol(x)))
colsum <- function(x) c(rep(1, nrow(x)) %*% x)
David
----------David Lubinsky---------------------------------------------
Email: david@hoqax.att.com
Phone: (908) 949-1722
Fax: (908) 949-7724
Mail: AT&T Bell Laboratories, Room 2J524, Crawfords Corner Rd,
Holmdel, NJ 07733
D
Technical Details
Message-ID:
<9201281531.AA02126@hoqas2.hoqas.att.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!uniwa!dmsperth!dmssyd.syd.dms.CSIRO.AU!newsShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fa931,e9f02f89e72966a3,start X-Google-Attributes: gidfa931,public X-Google-ArrivalTime: 1992-01-28 09:18:54 PST Path: arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!uniwa!dmsperth!dmssyd.syd.dms.CSIRO.AU!news From: hoqas!hoqas2!david@hoqax.att.com Newsgroups: aus.stats.s Subject: lists to arrays and row and column sums. Message-ID: <9201281531.AA02126@hoqas2.hoqas.att.com> Date: 28 Jan 92 16:04:32 GMT Sender: daemon@syd.dms.CSIRO.AU Organization: CSIRO Div. Maths & Stats, CSIRO Div. Applied Physics Lines: 32 X-Gateway: dmssyd.syd.dms.CSIRO.AU