]> code.communitydata.science - covid19.git/blob - wikipedia/example_analysis/pageview_example.R
revisions to reflect updated example filename and clean comments in R code
[covid19.git] / wikipedia / example_analysis / pageview_example.R
1 ### COVID-19 Digital Observatory
2 ### 2020-03-28
3 ### 
4 ### Minimal example analysis file using pageview data
5
6 library(tidyverse)
7 library(scales)
8
9 ### Import and cleanup one datafile from the observatory
10
11 DataURL <-
12     url("https://covid19.communitydata.science/datasets/wikipedia/digobs_covid19-wikipedia-enwiki_dailyviews-20200101.tsv")
13
14 views <-
15     read.table(DataURL, sep="\t", header=TRUE, stringsAsFactors=FALSE) 
16
17 ### Cleanup and do the grouping with functions from the Tidyverse
18 ### (see https://www.tidyverse.org for more info)
19
20 views <- views[,c("article", "project", "timestamp", "views")]
21 views$timestamp <- fct_explicit_na(as.character(views$timestamp))
22
23
24 ### Sorts and groups at the same time
25 views.by.proj.date <- arrange(group_by(views, project, timestamp),
26                         desc(views))
27
28 ### Export just the top 10 by pageviews
29 write.table(head(views.by.proj.date, 10),
30             file="output/top10_views_by_project_date.csv", sep=",",
31             row.names=FALSE)
32
33 ### A simple visualization
34 p <- ggplot(data=views.by.proj.date, aes(views))
35
36 ## Density plot with log-transformed axis
37 p + geom_density() + scale_x_log10(labels=comma)
38
39
40

Community Data Science Collective || Want to submit a patch?