[R] unique column values and counts

Ben Tupper btupper at bigelow.org
Tue Nov 30 03:37:19 CET 2010


Hi,

On Nov 29, 2010, at 7:40 PM, <topman77 at yahoo.com> wrote:

> How do I create a second dataframe that shows unique column values  
> from first dataframe and the number count of rows in first dataframe  
> where column value appears?
>
> For example, first dataframe is this:
>
>> x <- matrix(c(101:104,101:104,105:106,1:10), nrow=10, ncol=2)
>> x
>      [,1] [,2]
> [1,]  101    1
> [2,]  102    2
> [3,]  103    3
> [4,]  104    4
> [5,]  101    5
> [6,]  102    6
> [7,]  103    7
> [8,]  104    8
> [9,]  105    9
> [10,]  106   10
>>
>
>> From above dataframe I would like to generate count summary  
>> dataframe:
>
>      id    count
> [1,]  101    2
> [2,]  102    2
> [3,]  103    2
> [4,]  104    2
> [5,]  105    1
> [6,]  106    1
>
>

I think you want something like...

as.data.frame(table(x[,1]))
   Var1 Freq
1  101    2
2  102    2
3  103    2
4  104    2
5  105    1
6  106    1

Cheers,
Ben


Ben Tupper
Bigelow Laboratory for Ocean Sciences
180 McKown Point Rd. P.O. Box 475
West Boothbay Harbor, Maine   04575-0475
http://www.bigelow.org/



More information about the R-help mailing list