counting columns that match criteria

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

counting columns that match criteria

JL Villanueva
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: counting columns that match criteria

Jorge I Velez
Hi JL,

How about the following?

rowSums(d >1)  # d is your data

Best,
Jorge.-


On Thu, Nov 10, 2011 at 10:24 AM, JL Villanueva <> 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]]
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: counting columns that match criteria

jholtman
In reply to this post by JL Villanueva
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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: counting columns that match criteria

djmuseR
In reply to this post by JL Villanueva
Hi:

Here's a toy example:

# Default var names are V1-V20:
u <- as.data.frame(matrix(rpois(100, 3), ncol = 20))
u <- transform(u,
    ngt1 = apply(u[, c('V1', 'V4', 'V9', 'V15')], 1, function(x) sum(x > 1)) )
u

HTH,
Dennis

On Thu, Nov 10, 2011 at 7:24 AM, JL Villanueva <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.