[R] count number of groups

R. Michael Weylandt michael.weylandt at gmail.com
Fri May 25 18:59:10 CEST 2012


You'll have to be a little trickier if you want it to be smart and
pick up the name: [or I'm missing something obvious]

yourFunc <- function(x){
   dsx <- deparse(substitute(x))
   x <- length(unique(x))
   names(x) <- dsx
   x
}

yourFunc(ID)

yourFunc(ID^2)

yourFunc(ID[ID==2])


etc.

Hope this helps,
M

On Fri, May 25, 2012 at 12:42 PM, Charles Determan Jr <deter088 at umn.edu> wrote:
> Thank you Michael,
>
> However, this only provides the number of groups without a column label.  Is
> there a way to have it give the count with the 'ID' label?
>
> Regards,
> Charles
>
> On Fri, May 25, 2012 at 10:52 AM, R. Michael Weylandt
> <michael.weylandt at gmail.com> wrote:
>>
>> length(unique(ID))
>>
>> Michael
>>
>> On Fri, May 25, 2012 at 11:38 AM, Charles Determan Jr <deter088 at umn.edu>
>> wrote:
>> > Hello,
>> >
>> > Simple question that I am stuck on and can't seem to find an answer in
>> > the
>> > help files currently.  I have a list which contains repeated ID's.  I
>> > would
>> > like to have R count the number of ID's.  For example:
>> >
>> > ID=c(1,1,1,1,2,2,2,2,3,3,3,3)
>> > as.data.frame(ID)
>> >
>> > Clearly, there are 3 groups.  How would I have R give me the summary:
>> >
>> > ID
>> > 3
>> >
>> > Many thanks,
>> > Charles
>> >
>> >        [[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.
>
>



More information about the R-help mailing list