forked from 321k/Google-Trends
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Daily in percent
50 lines (40 loc) · 1.51 KB
/
Daily in percent
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
#Create download pahts
year=c(2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014)
output=vector()
downloadDir="C:/Users/erik.johansson/Downloads"
setwd(downloadDir)
for(i in year){
for(j in 1:12){
URL=URL_GT("ftse 100", year=i, month=j, length=2)
output=append(output, URL)
}
}
#Create table to store ouput
URL=output
gt_results=data.frame(as.Date("10.1.2004", "%d.%m.%Y"), NA, NA, NA, 1)
colnames(gt_results)=c("Date", "SVI", "Company", "Path", "Percentage")
for(i in 1:length(URL)){
#Download file
gt_path=downloadGT(URL[i], downloadDir)
#Format csv
gt_data=readGT(gt_path)
#Increment all by one to make percentage calculation possible
gt_data[,2]=gt_data[,2]+1
gt_data[which(is.na(gt_data[,2])),2]=1
gt_data[5]=NA
names(gt_data)[5]="Percentage"
#Calculate percentage change
for(j in 2:nrow(gt_data)){
gt_data$Percentage[j]=gt_data$SVI[j]/gt_data$SVI[j-1]
}
#Find first instance of date overlap in the new file
date_match=which(gt_data$Date==gt_results[nrow(gt_results),1])
#To ensure that we haven't skipped a date (since the data might be on a weekly level in some cases) we do the same check for the results data
date_match_results=which(gt_results$Date==gt_results[nrow(gt_results),1])
if(length(date_match)>0) {
gt_data_subset=gt_data[(date_match+1):nrow(gt_data),]
gt_results=gt_results[1:date_match_results,]
} else {gt_data_subset=gt_data}
colnames(gt_data_subset)=c("Date", "SVI", "Company", "Path", "Percentage")
gt_results=rbind(gt_results, gt_data_subset)
}