# [R] rowsum- is there a countsum

jim holtman jholtman at gmail.com
Sat Jan 26 15:48:43 CET 2008

```Is this what you are looking for in doing computations on the columns
of a dataframe?

> x <- data.frame(group=sample(1:2,10,TRUE),A=sample(1:4,10,TRUE),B=sample(1:5,10,TRUE),
+     C=sample(1:3,10,TRUE))
> x
group A B C
1      1 2 4 1
2      2 4 1 2
3      1 3 2 1
4      2 3 2 2
5      2 4 2 2
6      1 2 5 1
7      2 1 5 2
8      1 1 5 3
9      1 4 3 2
10     2 2 1 1
> # sum in each column
> aggregate(x[,-1], list(x\$group), sum)
Group.1  A  B C
1       1 12 19 8
2       2 14 11 9
> # count greater than 2
> aggregate(x[,-1], list(x\$group),function(.col) sum(.col>2))
Group.1 A B C
1       1 2 4 1
2       2 3 1 0
>
>

On Jan 26, 2008 9:13 AM, Anders Bjørgesæter <abjrges at getmail.no> wrote:
> Hello
>
> Is there an analogous function to "rowsum" that count the numbers
> according to a given vector (preferably larger than a given value)
>
> E.g. rowsum(x, group)
>
> X is the dataframe,     A       B       C
>                        5       0       0
>                        1       3       3
>                        0       1       12
> and group is the vector; 1,1,2
>
> rowsum gives:           A       B       C
>                1       6       3       3
>                2       0       1       12
>
>
> I want:                 A       B       C
>                1       2       1       1
>                2       0       1       1
>
> for e.g. count all > 0.
>
> Best Regards
> Anders
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

```