Skip to content

Instantly share code, notes, and snippets.

@amoeba
Created October 14, 2015 04:26
Show Gist options
  • Select an option

  • Save amoeba/8096cc36a7ee34158dcf to your computer and use it in GitHub Desktop.

Select an option

Save amoeba/8096cc36a7ee34158dcf to your computer and use it in GitHub Desktop.
dplyr's tally() function
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