Message-ID: <DB11A7D9-B558-47CC-B025-AE712291128F@comcast.net>
Date: 2012-09-16T21:36:26Z
From: David Winsemius
Subject: Count based on 2 conditions [Beginner Question]
In-Reply-To: <1347792105574-4643282.post@n4.nabble.com>
On Sep 16, 2012, at 3:41 AM, SirRon wrote:
> Hello,
> I'm working with a dataset that has 2 columns and 1000 entries. Column 1 has
> either value 0 or 1, column 2 has values between 0 and 10. I would like to
> count how often Column 1 has the value 1, while Column 2 has a value greater
> 5.
>
> This is my attempt, which works but doesn't seem to be very efficient,
> especially when testing different values or columns.
>
> count=0
> for (i in 1:1000) { if(dataset[i,2]>5 && ind[i,1]==1) { count=count+1}}
>
> I'm looking for a more efficient/elegant way to do this!
>
I see others have given you a solution using the vectorized sum function. I would have reached for 'table' and done it thusly:
table( one=dataset[,1], GT5=dataset[ , 2] > 5 )
--
David Winsemius, MD
Alameda, CA, USA