I have data in R that looks like this:
Cnty Yr Plt Spp DBH Ht Age
1 185 1999 20001 Bitternut 8.0 54 47
2 185 1999 20001 Bitternut 7.2 55 50
3 31 1999 20001 Pignut 7.4 71 60
4 31 1999 20001 Pignut 11.4 85 114
5 189 1999 20001 WO 14.5 80 82
6 189 1999 20001 WO 12.1 72 79
I would like to know the quantity of unique species (Spp) in each county (Cnty). "unique(dfname$Spp)" gives me a total count of unique species in the data frame, but I would like it by county.
Any help is appreciated! Sorry for the weird formatting, this is my first ever question on SO.
Thanks.
I wanted to add on to what A Handcart And Mohair mentioned. For those of you wanting to get the results of the code below into a data frame(helpful in R studio)...
You'll need to put the as.data.frame.matrix modifier in front of your code like so:
I was pretty new to R when I came upon this post, and it took me a long time to figure that out, so I thought I'd share.
As Justin mentioned aggregate is probably what you want. If you call your data frame foo, then the following should give you what you want, namely the number of individuals per species assuming that each row with Butternut represents a unique individual belonging to the butternut species. Note I used foo$Age to calculate the length of the vector, i.e. the number of individuals (row) belonging to each species, but you could use foo$Ht or foo$DBH etc.
Cheers,
Danny
We can now use the tally function to make this easier.
I've tried to make your sample data a little bit more interesting. Your sample data presently has just one unique "Spp" per "Cnty".
Next, as suggested,
tapply
is a good candidate here. Combineunique
andlength
to get the data you are looking for.If you're interested in simple tabulation (not of unique values), then you can explore
table
andftable
:A simple solution using the
data.table
approach.in case you want to reshape the output into a nicer table format:
The
dplyr::count()
function looks like a simple solution: