[R] counting columns that match criteria

jim holtman jholtman at gmail.com
Thu Nov 10 19:08:01 CET 2011


try this:

> x
  Col1 Col2 Col3 Col4
1    1    1    1    1
2    2    1    1    1
3    4    1    4    1
4    3    3    3    3
> apply(x, 1, function(a) sum(a > 1))
[1] 0 1 2 4
> x$count <- apply(x, 1, function(a) sum(a > 1))
> x
  Col1 Col2 Col3 Col4 count
1    1    1    1    1     0
2    2    1    1    1     1
3    4    1    4    1     2
4    3    3    3    3     4
>


On Thu, Nov 10, 2011 at 10:24 AM, JL Villanueva <jlpostage at gmail.com> wrote:
> Hi,
>
> I am a little new in R but I'm finding it extremely useful :)
>
> Here's my tiny question:
>
> I've got a table with a lot of columns. What I am interested now is to
> evaluate how many of 4 columns have a value greater than 1.
> I think it can be done with subset() but it will take a very long condition
> and become unfeasible if I want to compare more than 4 columns.
>
> I put here a small example
>
> Col1  Col2  Col3  Col 4
> 1        1       1       1             <-0 columns greater than 1
> 2        1       1       1             <-1 column greater than 1
> 4        1       4      1              <-2 columns greater than 1
> 3        3       3       3             <-3 columns greater than 1
>
> Then I want to filter by that number, my idea is to create a new column
> storing the number calculated and subset() by it.
>
> Any hints?
> Thanks in advance
>
> JL
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.



More information about the R-help mailing list