]> code.communitydata.science - covid19.git/blob - wikipedia/example_analysis/pageview_example.R
Merge pull request #15 from aaronshaw/master
[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-20200401.tsv")
13
14 views <-
15     read.table(DataURL, sep="\t", header=TRUE, stringsAsFactors=FALSE) 
16
17 ### Alternatively, uncomment and run if working locally with full git
18 ### tree
19 ###
20 ### Identify data source directory and file
21 ## DataDir <- ("../data/")
22 ## DataFile <- ("dailyviews2020032600.tsv")
23
24 ## related.searches.top <- read.table(paste(DataDir,DataFile, sep=""),
25 ##                                   sep="\t", header=TRUE,
26 ##                                   stringsAsFactors=FALSE)
27
28 ### Cleanup and do the grouping with functions from the Tidyverse
29 ### (see https://www.tidyverse.org for more info)
30
31 views <- views[,c("article", "project", "timestamp", "views")]
32 views$timestamp <- fct_explicit_na(views$timestamp)
33
34
35 ### Sorts and groups at the same time
36 views.by.proj.date <- arrange(group_by(views, project, timestamp),
37                         desc(views))
38
39
40 ### Export just the top 10 by pageviews
41 write.table(head(views.by.proj.date, 10),
42             file="output/top10_views_by_project_date.csv", sep=",",
43             row.names=FALSE)
44
45 ### A simple visualization
46 p <- ggplot(data=views.by.proj.date, aes(views))
47
48 ## Density plot with log-transformed axis
49 p + geom_density() + scale_x_log10(labels=comma)
50
51
52

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