r NHL-ALLSTAR聚类,helpers.r

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了r NHL-ALLSTAR聚类,helpers.r相关的知识,希望对你有一定的参考价值。

###############################################################################
## some helpers
###############################################################################

## helper function to parse results
parse_stattle <- function(stattle_list, entry) {
  x <- do.call("rbind", lapply(stattle_list, function(x) x[[entry]]))
  stopifnot(is.data.frame(x))
  return(x)
}


## helper function, count games from the gamelog, append to a dataframe
count_games <- function(player_slug) {
  gl_raw <- ss_get_result(ep="game_logs", 
                          query=list(player_id=player_slug), 
                          walk=TRUE,
                          verbose = FALSE)
  gl <- parse_stattle(gl_raw, "game_logs")
  player <- parse_stattle(gl_raw, "players")
  ## i believe gl's are already regular season
  x <- data.frame(id = player$id,
                  name = player$name,
                  slug = player_slug, 
                  games_played = nrow(gl))
  return(x)
}


## helper function for total stats-- binds to master df allstar
get_tot_stat <- function(stat, stat_type, player_list, allstar, parse_player=FALSE) {
  tmp_stats <- data.frame()
  for (player in player_list) {
    x <- ss_get_result(ep="total_stats", 
                       query=list(stat = stat,
                                  type = stat_type,
                                  player_id = player),
                       verbose=FALSE)
    players <- parse_stattle(x, "players")
    if (parse_player) {
      tmp_df <- data.frame(id = players$id,
                           pos = players$position_abbreviation,
                           salary = players$salary,
                           currency = players$salary_currency,
                           years = players$years_of_experience,
                           temp_stat = x[[1]]$total_player_stat$total)
      names(tmp_df)[ncol(tmp_df)] <- stat
      tmp_df <- unique(tmp_df)
    }
    if (parse_player == FALSE) {
      tmp_df <- data.frame(id = players$id,
                           temp_stat = x[[1]]$total_player_stat$total)
      names(tmp_df)[ncol(tmp_df)] <- stat
      tmp_df <- unique(tmp_df)
    }
    tmp_stats <- bind_rows (tmp_stats, tmp_df)
    cat("finished ", player, "\n")
  }
  allstar <<- left_join(allstar, tmp_stats)
}

以上是关于r NHL-ALLSTAR聚类,helpers.r的主要内容,如果未能解决你的问题,请参考以下文章

R语言聚类分析之层次聚类(hierarchical clustering)实战

68 R 聚类分析

R构建Kmeans聚类模型

R语言--聚类分析

R聚类(整群)抽样(Cluster Sampling)

R语言聚类分析之基于划分的聚类KMeans实战:基于菌株数据