To install prerequisites:
# install.packages(c("dplyr", "devtools", "lubridate"))
# devtools::install_github("metacran/cranlogs")
library(dplyr, warn.conflicts = FALSE)
library(cranlogs)
library(lubridate)
downloads <- cran_downloads(c("shiny", "dplyr", "knitr"), when = "last-month")
# We'll want to aggregate by week, so add a column indicating the week each day belongs to
downloads$week <- floor_date(downloads$date, unit = "week")
head(downloads)
## date count package week
## 1 2015-09-30 1829 shiny 2015-09-27
## 2 2015-10-01 1584 shiny 2015-09-27
## 3 2015-10-02 1555 shiny 2015-09-27
## 4 2015-10-03 869 shiny 2015-09-27
## 5 2015-10-04 850 shiny 2015-10-04
## 6 2015-10-05 1640 shiny 2015-10-04
# Loop over each unique package
resultList <- lapply(unique(downloads$package), function(pkg) {
# Loop over each of the unique weeks for the current package
weeks <- unique(downloads[downloads$package == pkg,]$week)
counts <- sapply(weeks, function(week) {
# Return the sum of the package downloads for the week
total <- sum(downloads[downloads$package == pkg & downloads$week == week,"count"])
})
# Format the results as a data frame
data.frame(package = pkg, week = weeks, count = counts, stringsAsFactors = FALSE)
})
# Combine the list of data frames into one big data frame
resultDF <- do.call(rbind, resultList)
# Order the results by week then package
resultDF[order(resultDF$week, resultDF$package),]
## package week count
## 6 dplyr 2015-09-27 10792
## 11 knitr 2015-09-27 11824
## 1 shiny 2015-09-27 5837
## 7 dplyr 2015-10-04 20880
## 12 knitr 2015-10-04 21913
## 2 shiny 2015-10-04 9898
## 8 dplyr 2015-10-11 22983
## 13 knitr 2015-10-11 21009
## 3 shiny 2015-10-11 9737
## 9 dplyr 2015-10-18 18937
## 14 knitr 2015-10-18 19084
## 4 shiny 2015-10-18 9013
## 10 dplyr 2015-10-25 12254
## 15 knitr 2015-10-25 11872
## 5 shiny 2015-10-25 8628
downloads %>%
# group_by indicates that the data should be broken up into groups, based
# on both package and week
group_by(package, week) %>%
# Summarize (collapse) the group, replacing the count column with the sum
# of the download counts
summarise(count = sum(count)) %>%
# Stop grouping (otherwise the arrange() below will only sort within each group)
ungroup() %>%
# Order the results by week then package
arrange(week, package)
## Source: local data frame [15 x 3]
##
## package week count
## (chr) (date) (dbl)
## 1 dplyr 2015-09-27 10792
## 2 knitr 2015-09-27 11824
## 3 shiny 2015-09-27 5837
## 4 dplyr 2015-10-04 20880
## 5 knitr 2015-10-04 21913
## 6 shiny 2015-10-04 9898
## 7 dplyr 2015-10-11 22983
## 8 knitr 2015-10-11 21009
## 9 shiny 2015-10-11 9737
## 10 dplyr 2015-10-18 18937
## 11 knitr 2015-10-18 19084
## 12 shiny 2015-10-18 9013
## 13 dplyr 2015-10-25 12254
## 14 knitr 2015-10-25 11872
## 15 shiny 2015-10-25 8628