尝试声明问题R

时间:2018-08-26 04:44:43

标签: r

这个简单的try()语句存在问题。我想要做的就是如果号码不存在或者出现错误,请移至下一个。 R的新手,我在某些文件夹中有一些信息,但在范围之间缺少一些数字。

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)
  )
}

1 个答案:

答案 0 :(得分:1)

请查看您共享的更正代码。 error参数(将处理引发的异常)应该在tryCatch调用中指出。请看如下:

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"))})
}