2

Having an issue with this simple try() statement. All I would like it to do is if the number is not there or if an error comes up move to the next. New to R and I have some info in certain folders but missing some numbers between the range.

library(readr)

season <- c(2014:2014)
gamenumbers <- c(20300:21271)
#############################################
# TEAM NULL DF's
season_teamstatsadj5v5 <- NULL


print('NUll DFs Created')
##############################################
for(game in gamenumbers){
  try(
    print('Start Team')
    print(as.character(game)) 
    ###################################################################################################################
    # team_stats_adj_5v5_df Bind
    teamstatsadj5v5<-paste0('//LVS_DB/Users/Mike/Desktop/NHL_PBP/', season,'/', game, '/', game, '_teamstatsadj5v5.csv')
    teamstatsadj5v5_df <- read_delim(teamstatsadj5v5, delim = ',')
    season_teamstatsadj5v5 <- rbind(season_teamstatsadj5v5, teamstatsadj5v5_df)
  )
}
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
Michael T Johnson
  • 679
  • 2
  • 13
  • 26

1 Answers1

1

Please see the corrected code you shared. error argument, which will handle the exception thrown should be indicated in tryCatch call. Please see as below:

library(readr)

season <- c(2014:2014)
gamenumbers <- c(20300:21271)
#############################################
# TEAM NULL DF's
season_teamstatsadj5v5 <- NULL


print('NUll DFs Created')
##############################################
for(game in gamenumbers){
  tryCatch({
    print('Start Team')
    print(as.character(game)) 
    ###################################################################################################################
    # team_stats_adj_5v5_df Bind
    teamstatsadj5v5<-paste0('//LVS_DB/Users/Mike/Desktop/NHL_PBP/', season,'/', game, '/', game, '_teamstatsadj5v5.csv')
    teamstatsadj5v5_df <- read_delim(teamstatsadj5v5, delim = ',')
    season_teamstatsadj5v5 <- rbind(season_teamstatsadj5v5, teamstatsadj5v5_df)
  }, error = function(e) {message(paste0(e, "\n"))})
}
Artem
  • 3,304
  • 3
  • 18
  • 41