Re: [R] Subsetting Data

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Thu, 28 Apr 2011 15:43:17 -0400

On Apr 28, 2011, at 3:38 PM, David Winsemius wrote:

>
> On Apr 28, 2011, at 3:13 PM, Abraham Mathew wrote:
>
>> I'm using the subset() function in R.
>>
>> dat <- data.frame(one=c(6,7,8,9,10), Number=c(5,15,13,1,13))
>>
>> subset(dat, Number >= 10)
>>
>> However, I want to find the number of all rows who meet the
>> Number>=10
>> condition.
>>
>> I've done this in the past with something like colSums or rowSums
>> or another
>> similar function.
>> But I don't remember how to get the number of elements which meet
>> that
>> condition.
>>
>> function(subset(dat, Number >= 10)) #function is what i'm asking
>> about
>
> Daily's answer would work but if you wanted a direct answer to your
> question then try nrow or NROW:
>
> > nrow(subset(dat, Number >= 10))
> [1] 3
>
> > NROW(subset(dat, Number >= 10))
> [1] 3
>
> There is a difference and I am under the impression that NROW is
> safer in some way.

 > NROW
function (x)
if (is.array(x) || is.data.frame(x)) nrow(x) else length(x)

So if you were doing something with tapply or a [,] subsetting operation which might return a single dimensioned result you would definitely want NROW.

>
>
>>
>> In the above example, I'd run get 3 because there are 3 Number values
>> greater than 10.
>
>
> --
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



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. Received on Thu 28 Apr 2011 - 19:45:32 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 28 Apr 2011 - 19:50:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive