-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfantasy_test.R
48 lines (28 loc) · 1.21 KB
/
fantasy_test.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
fantasy <- function ()
{
#Load libraries
rm(list=ls())
library("XML")
all_fantasy_data_frame <- data.frame()
#loop through all sites to get the data 25 is enough
for (i in 1:25) {
#read in string
statistics_string <- paste("http://fantasy.premierleague.com/stats/elements/?stat_filter=total_points&element_filter=0&page=",i,sep="")
#read data
fantasy_data <- readHTMLTable(statistics_string, stringsAsFactors = FALSE)
#Turn data into data frame the data is stored in the first elemnt of the table as a list
fantasy_data_frame <- data.frame(fantasy_data[1])
#The first 2 columns are blank. fix that only if the vector is not empty.
if (length(fantasy_data_frame) > 0 )
{
fantasy_data_frame <- fantasy_data_frame[,3:9]
}
#lets name the columns withou the null
names(fantasy_data_frame) <- sub("NULL.","",names(fantasy_data_frame))
all_fantasy_data_frame <- rbind(all_fantasy_data_frame,fantasy_data_frame)
all_fantasy_data_frame$Price<-gsub("£","",all_fantasy_data_frame$Price)
all_fantasy_data_frame$Total<-as.numeric(all_fantasy_data_frame$Total)
all_fantasy_data_frame$Price<-as.numeric(all_fantasy_data_frame$Price)
}
return(all_fantasy_data_frame)
}