counting the number of rows that satisfy a certain criteria

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

counting the number of rows that satisfy a certain criteria

Beejai
I have 4 columns, and about 300K plus rows with 0s and 1s.

I'm trying to count how many rows satisfy a certain criteria... for
instance, how many rows are there that have the first column == 1 as
well as the second column == 1.

I've tried using rowSums and colSums but it keeps giving me this type of error:

Error in rowSums(X[1] == 1 & X[2] == 1) :
  'x' must be an array of at least two dimensions

Thanks in advance!

______________________________________________
[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 the number of rows that satisfy a certain criteria

Jorge I Velez
Hi Kate,

You could try

sum(X[, 1] == 1 &  X[, 2] == 1)

where X is your data set.

HTH,
Jorge.-



On Sun, Jun 22, 2014 at 12:57 AM, Kate Ignatius <[hidden email]>
wrote:

> I have 4 columns, and about 300K plus rows with 0s and 1s.
>
> I'm trying to count how many rows satisfy a certain criteria... for
> instance, how many rows are there that have the first column == 1 as
> well as the second column == 1.
>
> I've tried using rowSums and colSums but it keeps giving me this type of
> error:
>
> Error in rowSums(X[1] == 1 & X[2] == 1) :
>   'x' must be an array of at least two dimensions
>
> Thanks in advance!
>
> ______________________________________________
> [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 the number of rows that satisfy a certain criteria

Beejai
Thanks!

On Sat, Jun 21, 2014 at 11:05 AM, Jorge I Velez
<[hidden email]> wrote:

> Hi Kate,
>
> You could try
>
> sum(X[, 1] == 1 &  X[, 2] == 1)
>
> where X is your data set.
>
> HTH,
> Jorge.-
>
>
>
> On Sun, Jun 22, 2014 at 12:57 AM, Kate Ignatius <[hidden email]>
> wrote:
>>
>> I have 4 columns, and about 300K plus rows with 0s and 1s.
>>
>> I'm trying to count how many rows satisfy a certain criteria... for
>> instance, how many rows are there that have the first column == 1 as
>> well as the second column == 1.
>>
>> I've tried using rowSums and colSums but it keeps giving me this type of
>> error:
>>
>> Error in rowSums(X[1] == 1 & X[2] == 1) :
>>   'x' must be an array of at least two dimensions
>>
>> Thanks in advance!
>>
>> ______________________________________________
>> [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.
Reply | Threaded
Open this post in threaded view
|

Re: counting the number of rows that satisfy a certain criteria

arun kirshna
In reply to this post by Beejai
Hi,
Try:
set.seed(42)
 X <- as.data.frame(matrix(sample(0:1, 4*50,replace=TRUE), ncol=4))
 table(X[1:2])[4]
#[1] 15

sum(rowSums(X[1:2])==2)
#[1] 15
A.K.




On Saturday, June 21, 2014 10:59 AM, Kate Ignatius <[hidden email]> wrote:
I have 4 columns, and about 300K plus rows with 0s and 1s.

I'm trying to count how many rows satisfy a certain criteria... for
instance, how many rows are there that have the first column == 1 as
well as the second column == 1.

I've tried using rowSums and colSums but it keeps giving me this type of error:

Error in rowSums(X[1] == 1 & X[2] == 1) :
  'x' must be an array of at least two dimensions

Thanks in advance!

______________________________________________
[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.