If you prefer to do it with base functions, you could start by
thinking about one column at a time, for which the tapply function is
a logical choice:
> tapply(d[,"A"], e, sum)
r s
3 6
Then wrap that in an apply call that handles each column sequentially:
> apply(d, 2, function (x) tapply(x, e, sum) )
A B C
r 3 4 3
s 6 3 4
If you wanted a count of other logical "events", say the number of
zeros, you could "sum" a logical function as s:
> apply(d, 2, function (x) tapply(x, e, function (y) sum(y == 0)))
A B C
r 7 6 7
s 4 7 6
--
On Mar 8, 2009, at 1:23 PM, baptiste auguie wrote:
Hi,
You could use the reshape package:
d$e <- e
recast(d, variable~e, fun=sum)
The doBy package is another option.
baptiste
On 8 Mar 2009, at 17:14, soeren.vo...@eawag.ch wrote:
A dataframe holds 3 vars, each checked true or false (1, 0). Another
var holds the grouping, r and s:
### start:example
set.seed(20)
d <- data.frame(sample(c(0, 1), 20, replace=T), sample(c(0, 1), 20,
replace=T), sample(c(0, 1), 20, replace=T))
names(d) <- c("A", "B", "C")
e <- rep(c("r", "s"), 10)
### end:example
How do I get the count of "1's" (or any other function) applied over
each var according to the grouping? That is:
Desired output table:
A B C
r count count count
s ... ... ...
or likewise transposed. I'd like to use the table for textual display
and/or barplot creation.
Thx, Sören
--
Sören Vogel, PhD-Student, Eawag, Dept. SIAM
http://www.eawag.ch, http://sozmod.eawag.ch
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
_____________________________
Baptiste Auguié
School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK
Phone: +44 1392 264187
http://newton.ex.ac.uk/research/emag
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.