Skip to content

Instantly share code, notes, and snippets.

@dmarcelinobr
Created January 1, 2016 21:48
Show Gist options
  • Save dmarcelinobr/f554dbb29e32472ebd3a to your computer and use it in GitHub Desktop.
Save dmarcelinobr/f554dbb29e32472ebd3a to your computer and use it in GitHub Desktop.
dplyr's tally()
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