# [R] Conditional Selection of Columns for Tables

Gabor Grothendieck ggrothendieck at gmail.com
Sat Jan 27 21:18:41 CET 2007

```Try these:

aggregate(DF[4], DF[c(3,5)], sum)

xtabs(revenue ~ status + name, DF)

library(doBy)
summaryBy(revenue ~ status + name, data = DF, FUN = sum)

On 1/27/07, Mark Hempelmann <neo27 at rakers.de> wrote:
> Dear Wizards -
>
>        Thank you so much for your help. That was exactly what I was looking
> for. Now, I have been working on conditional selection of columns in a
> data frame. My goal is to calculate the total revenue per sales
> representative per status in a table. I have come to a complete stop:
>
> Let's say, we have a data.frame called df with several columns and a
> number of rows:
>
> df <- data.frame( nr=101:110, letter=LETTERS[1:10],
> name=c(rep("eenie",3), rep("meenie",2), rep("miney",4),
>   "moe"), revenue=round(runif(10, min=100, max=1000),0),
> status=round(runif(10,min=1, max=3),0) )
>
> gives
>     nr letter   name revenue status
> 1  101      A  eenie     764      2
> 2  102      B  eenie     918      2
> 3  103      C  eenie     936      3
> 4  104      D meenie     770      2
> 5  105      E meenie     280      1
> 6  106      F  miney     172      2
> 7  107      G  miney     439      2
> 8  108      H  miney     607      1
> 9  109      I  miney     553      1
> 10 110      J    moe     322      2
>
> where status means: 3=no deal, 2=pending, 1=good job.
> now, we want the total revenue per sales representative per status in a
> table.
>
> sum( subset(df, name=="eenie", select=revenue) )
>
> gives the total of eenie without status, but I would like to have sthg like:
>
> status
> 1
> name   revenue
> eenie  1000
> meenie 2000...
>
> status
> 2
> name   revenue
> eenie  100
> meenie 200...
>
> Are these flat contingency tables? How can I get the results without
> much hazzle in one list/ table? i did read the ?ftable and what I was
> able to derive so far is:
>
> flat.df <- ftable(df[c("name", "revenue", "status")])
>
> but I am unable to further agglomerate the data. hmpf.
> Good God, what would I do without my R-help forum?
>
> Thank you again
> Cheers and a relaxing weekend
> mark
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help