Scraping data using FPL API and construct a dataframe or a matrix in r

What I'm trying to achieve is to fetch the total points data for each player per game week in the fantasy premier league and construct it in the following way with a dataframe or a matrix:

 Player_name_1 Player_name_2 ... Player_name_n Gameweek_1 Gameweek_2 ... Gameweek_m 

I'm using the following data sources: for the points per player per Gameweek for the id and number of each player

I've been using the following code to fetch the points for each player for gameweeks 1 and 2 but don't understand why there is a difference in number of items -> Gameweek1 has 524 and Gameweek has 537.

I'm also trying to wrap my mind around how to join the id and/or names of the players from the bootstrap-static data source with the data from the event data source to create the data frame or matrix demonstrated above.

# Load the jsonlite package library(jsonlite) # Fetch points per player for Gameweek 1 and 2 Data_GW_1 <- fromJSON("") points_GW1 <- sapply(Data_GW_1$elements, function(x) x$stats$total_points, USE.NAMES = FALSE) Data_GW_2 <- fromJSON("") points_GW2 <- sapply(Data_GW_2$elements, function(x) x$stats$total_points, USE.NAMES = FALSE) # Create names before joining data GW1_names <- "Gameweek 1" GW2_names <- "Gameweek 2" # Create a dataframe from the data with names df <- data.frame(points_GW1,points_GW2) names(df) <- c(GW1_names, GW2_names) print(df) # Getting the following error message Error in data.frame(points_GW1, points_GW2) : arguments imply differing number of rows: 524, 537 > names(df) <- c(GW1_names, GW2_names) Error in names(df) <- c(GW1_names, GW2_names) : names() applied to a non-vector > print(df) function (x, df1, df2, ncp, log = FALSE) { if (missing(ncp)) .Call(C_df, x, df1, df2, log) else .Call(C_dnf, x, df1, df2, ncp, log) } <bytecode: 0x000000000428a038> <environment: namespace:stats> 

Hope this makes some sense :)

2 Answers

Several things:

  • Minor thing is you seem to be getting a name-collision on df. You try to assign a dataframe df <- data.frame(points_GW1,points_GW2), the error messages later on seem to say you have a name collision with the builtin function stats::df (Error: names() applied to a non-vector tells you that df is still stats::df(), not your intended dataframe), but that code should work fine and your assignment should just mask (overwrite) the builtin. I tried your code and it did work. So rerun and check that, and if it still causes grief, just rename the damn dataframe already, e.g. dfs,dat. (Be aware that if you're doing the assign df <- data.frame... inside some function of yours, it won't get assigned in the parent (global) environment, that's what <<- is for. But keep your life easy, avoid the name conflict.)

  • Now, the far more important error is that data.frame(points_GW1, points_GW2) fails because, as it's telling you, they have a different number of rows: 524, 537. Clearly fromJSON() on those two webpages is returning a different number of rows. That's not necessarily wrong, you need to actually dig in and investigate this yourself, figure out how to parse them. I tried playing with different settings e.g. fromJSON(..., flatten=F) and simplifyDataFrame and I couldn't easily understand the structure of the webpages. That's for you to figure out, not for us. Pleas update the question details if you make progress.

    e.g. I tried fromJSON("", simplifyDataFrame=T) [1]

Fantasy Premier League often adds and removes players from the game between gameweeks. That is probably the cause for the increase in the number of players.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like