Skip to content

Instantly share code, notes, and snippets.

@artemklevtsov
Last active September 8, 2017 17:44
Show Gist options
  • Save artemklevtsov/cf6d82cbd8afe253a0213ef7648cd74d to your computer and use it in GitHub Desktop.
Save artemklevtsov/cf6d82cbd8afe253a0213ef7648cd74d to your computer and use it in GitHub Desktop.
library(curl)
library(jsonlite)
library(data.table)
# Get data
hero_stats <- stream_in(curl("https://api.opendota.com/api/heroStats"), verbose = FALSE)
# Post processing
setDT(hero_stats)
hero_roels <- dcast(
data = res[, .(roles = unlist(roles)), by = id],
formula = id ~ roles,
fun.aggregate = length,
value.var = "roles"
)
setnames(hero_roels, names(hero_roels)[-1], paste0(tolower(names(hero_roels)[-1]), "_role"))
cols <- grep("_role", names(hero_roels), value = TRUE)
hero_roels[, (cols) := lapply(.SD, as.logical), .SDcols = cols]
hero_stats[, roles := NULL]
hero_stats <- merge(hero_stats, hero_roels, by = "id", all.x = TRUE)
hero_stats[, primary_attr := as.factor(primary_attr)]
hero_stats[, attack_type := as.factor(attack_type)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment