lundi 28 octobre 2019

Stuck trying to use ! operator on a date in an R If Statement

i'm trying to extract NBA gamelog data and store it into mysql. The code works, i have everything working fine, it's just that when i run it i get null values in MySQL because it gives me NULL gamelogs from TODAY's Games that obviously haven't been played yet. the package i'm using to extract this data knows that games are going to be played tonight, so it includes them when i scrape the data. i don't want a bunch of null values in my database so i'm trying to find a fix.

gameLogs <- game_logs(seasons=2020, league="NBA", result_types="player", season_types="Regular Season")
for(i in 1:nrow(gameLogs)){
    currentLine <- as.data.frame(gameLogs[i,])
    playerID <- gameLogs$idPlayer[i]
    dbWriteTable(con=basketball, value=currentLine, name=paste0("gamelogs"), append=TRUE, overwrite=FALSE)
    message(i)
}

Below is the solution I tried using. I basically want the code to run for every gamelog EXCEPT for today's date, but i can't use the ! (NOT operator?) with a date in R. i get the following error: Error in Ops.Date(today) : unary ! not defined for "Date" objects.

today <- Sys.Date()
gameLogs <- game_logs(seasons=2020, league="NBA", result_types="player", season_types="Regular Season")
for(i in 1:nrow(gameLogs)){
  if(gameLogs$dateGame[i] == !today){
    currentLine <- as.data.frame(gameLogs[i,])
    playerID <- gameLogs$idPlayer[i]
    dbWriteTable(con=basketball, value=currentLine, name=paste0("gamelogs"), append=TRUE, overwrite=FALSE)
    message(i)
  }
}

Does that make sense? the dates are exact matches, Sys.Date() gives me a value of "2019-10-28" which is exactly how the format appears in the gamelogs. if anyone was able to follow along and is able to help find a solution for me i'd appreciate it !

Aucun commentaire:

Enregistrer un commentaire