-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.R
More file actions
93 lines (63 loc) · 2.6 KB
/
analysis.R
File metadata and controls
93 lines (63 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
library(data.table)
library(ggplot2)
library(reshape)
theme_set(theme_bw())
d <- read.delim("mail_metadata.tsv", header=FALSE,
col.names=c("flags", "timestamp", "precedence", "google"))
d$timestamp <- as.POSIXct(d$timestamp, tz="UTC",
origin=as.POSIXct("1970-01-01 00:00:00"))
# limit the dataset to emails sent post timestamp
d <- d[d$timestamp > as.POSIXct("2004-04-01 00:00:00"),]
d$week <- cut(d$timestamp, breaks="weeks")
# list and then drop list mail
table(d$precedence)
d <- d[is.na(d$precedence),]
d$precedence <- NULL
d$replied <- grepl('R', d$flags)
google.by.week <- function (d) {
setDT(d)
weeks <- d[,list(total=length(google), google=table(google)["TRUE"]), by=week]
# drop things
weeks <- weeks[weeks$total > 1,]
weeks$google.prop <- weeks$google / weeks$total
weeks$week <- as.Date(as.character(weeks$week))
return(weeks)
}
# find proportions per year
replied <- google.by.week(d[d$replied,])
replied <- replied[complete.cases(replied),]
replied.tbl <- as.data.frame(
tapply(replied$google, substr(as.character(replied$week), 1, 4), sum) /
tapply(replied$total, substr(as.character(replied$week), 1, 4), sum))
colnames(replied.tbl) <- "prop.google"
replied.tbl$year <- row.names(replied.tbl)
row.names(replied.tbl) <- NULL
ggplot(data=replied.tbl) + aes(x=year, y=prop.google) +
geom_bar(stat="identity")
replied.tbl
# Graph #1: Emails from Google Over Time
#######################################################
raw.data <- google.by.week(d)
raw.data$google.prop <- NULL
raw.data <- melt(raw.data, id.var="week")
pdf(file="emails_gmail_over_time.pdf", width=10, height=6)
ggplot(data=raw.data) + aes(x=week, y=value, color=variable, group=variable) +
geom_point() +
stat_smooth(method="loess", show_guide=FALSE) +
scale_color_discrete("", breaks=c("total", "google"),
labels=c("All Emails", "From Google")) +
scale_x_date("Date") +
scale_y_continuous("Number of Emails")
dev.off()
# Graph #2: Proportions of Email from Google
#######################################################
prop.data <- rbind(cbind(google.by.week(d), subset="All Email"),
cbind(google.by.week(d[d$replied]), subset="Email with Replies"))
pdf(file="emails_gmail_prop_over_time.pdf", width=10, height=8)
ggplot(data=prop.data) + aes(x=week, y=google.prop, size=total, group=subset) +
geom_point() + facet_grid(subset~.) +
scale_y_continuous("Proportion from Google", limits=c(0,1)) +
scale_x_date("Date") +
scale_size("Emails") +
stat_smooth(method="loess", show_guide=FALSE)
dev.off()