-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstagram_average_function.R
89 lines (50 loc) · 2.09 KB
/
instagram_average_function.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
# -------------------------------------------------------------
# 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)
# Function ------------------------------------------------------------
CalcAverageVisitors <- function(day,hour){
# Calculate the average number of visitors
#
# Args:
# day: string ('Sat','Sun','Mon','Tues','Wed','Thurs','Fri')
# hour: numeric 24 horus
# Returns:
# The percentage of visitors
# 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
# hoursOpen <- 8*6 + 11
# average.TotalVisitors <- totalNum.openingHours/hoursOpen
totalNum.openingHours <- num.Fri.hours + num.notFri.hours
counts <- table( hours.and.days[hours.and.days$day == day
& hours.and.days$hour == hour ,] )
averages <- counts[1]/totalNum.openingHours
averages <- averages *100
}