-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstagram_average.R
106 lines (58 loc) · 2.62 KB
/
instagram_average.R
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
94
95
96
97
98
99
100
101
102
103
# -------------------------------------------------------------
# Sahil Shah <[email protected]>
# Tues, 9/22/15
# -------------------------------------------------------------
# Get --------------------------------------------------------------
library(RCurl)
text.file <- getURL("https://raw.githubusercontent.com/etsuprun/crowdattraction/master/instagram.txt")
instagram.data <- read.table(text = text.file,
header=TRUE,
sep= "",
stringsAsFactors=FALSE)
# Mung --------------------------------------------------------------
# Number of posts per day of the week
# table(instagram.data$wday)
hours <- sapply(instagram.data$date,function(insta.date){
# Pull time out from date
time <- strsplit(insta.date," ")[[1]][2]
# Pull hour out from time
hour <- as.numeric(strsplit(time, ":")[[1]][1])
})
days <- instagram.data$wday
hours.and.days <- data.frame(day = days,
hour = hours,
stringsAsFactors = FALSE)
# --------------------------------------------------------------------
# Calculate number of people at day,hour and average number over
# opening hours
# Number of Instagrams during Friday opening hours (9 am to 8 pm)
num.Fri.hours <- dim( hours.and.days[hours.and.days$day == "Fri"
& hours.and.days$hour >= 9
& hours.and.days$hour <= 20,] )[1]
# Number of Instagrams during Friday opening hours (9 am to 5 pm)
num.notFri.hours <- dim(hours.and.days[hours.and.days$day != "Fri"
& hours.and.days$hour >= 9
& hours.and.days$hour <= 17,] )[1]
totalNum.openingHours <- num.Fri.hours + num.notFri.hours
# Plot ----------------------------------------------------------------
par(mfrow=c(3,3))
for (day in c('Sat','Sun','Mon','Tues','Wed','Thurs')) {
counts <- table( hours.and.days[hours.and.days$day == day
& hours.and.days$hour >= 9
& hours.and.days$hour <= 17,] )
averages <- counts[1:length(counts)]/totalNum.openingHours
averages <- averages *100
plot(averages,main=day,ylim=c(0,5),xaxt='n',ylab='Percentage of Vistors')
axis(side = 1, at = c(1:9), labels= c("9","10","11","12","13",
"14","15","16","17"))
}
# Friday
counts <- table( hours.and.days[hours.and.days$day == 'Fri'
& hours.and.days$hour >= 9
& hours.and.days$hour <= 20,] )
averages <- counts[1:length(counts)]/totalNum.openingHours
averages <- averages *100
plot(averages, main='Fri',ylim=c(0,5),xant='n',ylab='Percentage of Vistors')
axis(side = 1, at = c(1:12), labels= c("9","10","11","12","13",
"14","15","16","17","18","19","20"))
# ------------------------------------------------------------------