Created
October 14, 2015 04:26
-
-
Save amoeba/8096cc36a7ee34158dcf to your computer and use it in GitHub Desktop.
dplyr's tally() function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(dplyr) | |
| # Set up an example data frame | |
| stations = c("A", "B", "C") | |
| species = c("M", "N", "O", "P", "Q") | |
| baseline = c(5, 50, 500) | |
| n <- 25 | |
| mydf <- data.frame(station=sample(stations, n, replace=TRUE), species=sample(species, n, replace=TRUE)) | |
| mydf$baseline <- baseline[match(mydf$station, stations)] | |
| mydf[order(mydf$station, mydf$species),] | |
| # Count by stations+species | |
| mydf %>% group_by(station, species) %>% tally() | |
| # Count by station+species, weighted by 1/baseline | |
| mydf %>% group_by(station, species) %>% tally(wt=1/baseline) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment