Re: [R] Count of unique factors within another factor

From: Henrique Dallazuanna <wwwhsd_at_gmail.com>
Date: Mon, 14 Jun 2010 13:44:33 -0300

Another possibility:

 rowSums(table(x) > 0)

On Sun, Jun 13, 2010 at 3:08 PM, Erik Iverson <eriki_at_ccbr.umn.edu> wrote:

> I think ?tapply will help here. But *please* read the posting guide and
> provide minimal, reproducible examples!
>
>
> Birdnerd wrote:
>
>> I have a data frame with two factors (sampling 'unit', 'species'). I want
>> to
>> calculate the number of unique 'species' per 'unit.' I can calculate the
>> number of unique values for each variable separately, but can't get a
>> count
>> for each ‘unit’.
>>
>> data=read.csv("C:/Desktop/sr_sort_practice.csv")
>>> attach(data)
>>>
>>
>> data[1:10,]
>>>
>> unit species
>> 1 123 ACMA
>> 2 123 LIDE
>> 3 123 LIDE
>> 4 123 SESE
>> 5 123 SESE
>> 6 123 SESE
>> 7 345 HEAR
>> 8 345 LOHI
>> 9 345 QUAG
>> 10 345 TODI…..
>>
>> sr.unique<- lapply (data, unique)
>>>
>> $unit
>> [1] 123 345 216
>> $species
>> [1] ACMA LIDE SESE HEAR LOHI QUAG TODI UMCA ARSP LIDE
>>
>> sapply (sr.unique,length)
>>>
>> unit species 3 10
>>
>> Then, I get stuck here because this unique species count is not given for
>> each ‘unit’.
>> What I'd like to get is:
>>
>> unit species
>> 123 3
>> 345 4
>> 216 --
>>
>> Thanks--
>>
>>
> ______________________________________________
> 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.
>

-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

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

Received on Mon 14 Jun 2010 - 16:47:37 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 Mon 14 Jun 2010 - 17:00:30 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