Skip to content

Instantly share code, notes, and snippets.

@vpnagraj
Created November 9, 2016 14:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vpnagraj/98d06c76e49034834b1f811e2bc299b1 to your computer and use it in GitHub Desktop.
Save vpnagraj/98d06c76e49034834b1f811e2bc299b1 to your computer and use it in GitHub Desktop.
# list manipulation workshop
# scratch script
# november 9 2016
#############################################
slamwins <- list(17,14,14,12,11)
#############################################
class(slamwins)
#############################################
slamwins
#############################################
slamwins[[2]][1]
#############################################
names(slamwins) <- c("Federer", "Sampras", "Nadal", "Djokovic", "Borg")
slamwins
#############################################
slamwins <- list(Federer = 17, Sampras = 14, Nadal = 14, Djokovic = 12, Borg = 11)
slamwins
#############################################
slamwins$Federer
# federer has ? more titles than borg
slamwins$Federer - slamwins$Borg
#############################################
slamwins <-
list(
Federer =
list(
AUS = 4,
FR = 1,
WIM = 7,
US = 5),
Sampras =
list(
AUS = 2,
FR = 0,
WIM = 7,
US = 5),
Nadal =
list(
AUS = 1,
FR = 9,
WIM = 2,
US = 2),
Djokovic =
list(
AUS = 6,
FR = 1,
WIM = 3,
US = 2),
Borg =
list(
AUS = 0,
FR = 6,
WIM = 5,
US = 0)
)
#############################################
totals <- c(17, 14, 14, 12, 11)
for (i in 1:length(slamwins)) {
slamwins[[i]]$Total <- totals[i]
}
slamwins
#############################################
for (i in 1:length(slamwins)) {
slamwins[[i]]$Total <- NULL
}
#############################################
lapply(slamwins, unlist)
#############################################
lapply(lapply(slamwins, unlist), sum)
#############################################
slamwins <- lapply(lapply(slamwins, unlist), function(x) c(x, Total = sum(x)))
slamwins
#############################################
as.data.frame(slamwins)
#############################################
datmat <- do.call(rbind, slamwins)
datdf <- as.data.frame(datmat, row.names = FALSE)
datdf$player <- row.names(datmat)
datdf
#############################################
# install.packages("jsonlite)
library(jsonlite)
had1 <- fromJSON("https://api.github.com/users/hadley/repos?page=1&per_page=100", simplifyVector = FALSE)
had2 <- fromJSON("https://api.github.com/users/hadley/repos?page=2&per_page=100", simplifyVector = FALSE)
#############################################
had <- c(had1,had2)
rm(had1, had2)
#############################################
length(had)
#############################################
had[[1]]
#############################################
had[[5]]$language
#############################################
sapply(had, function(x) x$watchers)
#############################################
names(had) <- sapply(had, function(x) x$name)
sapply(had, function(x) x$watchers)
#############################################
# exercise
# - How many times are these repositories forked on average?
# - Is Hadley a "site_admin" on any of these repositories?
# - Try reading the data from Github again. Make sure to use the `simplifyVector = TRUE` argument instead. What happened?
#############################################
# install.packages("rlist")
library(rlist)
list.map(had, created_at)
list.sort(had, forks_count)
list.filter(had, size > 50000)
list.group(had, language)
list.table(had, fork)
#############################################
# install.packages("purrr")
library(purrr)
?map
?transpose
?flatten
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment