Last active
October 17, 2016 11:17
-
-
Save patternproject/a8144e25279bf5421f536f4ce131e3dc to your computer and use it in GitHub Desktop.
df (df.nbl.win.2) contains country, yr1, y2 and yr3. Helper / Workhorse fn (fn_get_rank) takes country and year, returning rank. Using pmap I need to rename cols for proper mapping in pmap.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ------------------- | |
# HELPER FUNCTION | |
# ------------------- | |
# fn to search rank | |
# INPUT: year (integer) | |
# OUTPUT: rank (integer) | |
# SRC: | |
# http://adv-r.had.co.nz/Functions.html | |
# https://cran.r-project.org/web/packages/ArgumentCheck/vignettes/ArgumentChecking.html | |
# fn_get_rank = function (c.country = "finland", i.search.year = 2008) # for testing | |
fn_get_rank = function (c.country, i.search.year) | |
{ | |
print("entering fn") | |
# if input args are missing | |
# SRC: | |
# https://www.r-bloggers.com/programming-with-r-%E2%80%93-checking-function-arguments/ | |
if (missing(c.country)) | |
stop("Need to specify country for calculations.") | |
if (missing(i.search.year)) | |
stop("Need to specify year for calculations.") | |
if (!is.character(c.country)) | |
stop("Country should be character.") | |
if (!is.numeric(i.search.year)) | |
stop("Year should be numeric.") | |
# search the year in df.1 | |
# if found return corresponding rank | |
# else return NA | |
i.flag = | |
df.1 %>% | |
filter(year == i.search.year & country == c.country) %>% | |
count() %>% | |
unlist(use.names = FALSE) # to convert df into a vector **1 | |
# src: for **1 | |
# http://stackoverflow.com/questions/21618423/extract-a-dplyr-tbl-column-as-a-vector | |
#str(i.flag) | |
#print(i.flag) | |
if (i.flag == 1) | |
{ | |
return( | |
df.1 %>% | |
filter(year == i.search.year & country == c.country) %>% | |
select(rank) %>% | |
unlist(use.names = FALSE) | |
) | |
} | |
else | |
{ | |
return(NA) | |
} | |
} # end fn_get_rank | |
# ------------------- | |
# dput the data structure / df used below | |
# ------------------- | |
# > dput(df.nbl.win.1) | |
structure(list(c.country = c("finland", "usa", "china", "liberia", | |
"yemen", "cross-national", "cross-national", "india", "pakistan", | |
"tunisia", "colombia"), i.search.year = c(2008L, 2009L, 2010L, | |
2011L, 2011L, 2012L, 2013L, 2014L, 2014L, 2015L, 2016L)), class = "data.frame", row.names = c(NA, | |
-11L), .Names = c("c.country", "i.search.year")) | |
# ------------------- | |
# ACTUAL CODE FOLLOWS | |
# ------------------- | |
# Naming cols is necessary | |
# https://github.com/hadley/purrr/issues/203 | |
names(df.nbl.win.1) = c("c.country", "i.search.year") | |
# extending the df to include year2 and year3, post winning the medal | |
df.nbl.win.2 = | |
df.nbl.win.1 %>% | |
group_by(c.country) %>% | |
mutate(year2 = i.search.year + 1, | |
year3 = i.search.year + 2) %>% | |
ungroup() | |
# mapping medal year >> ITERATION 1 | |
df.nbl.win.3$rank.in.nbl.yr = | |
df.nbl.win.3 %>% | |
select(c.country,i.search.year) %>% | |
pmap(fn_get_rank) %>% | |
unlist() | |
# mapping medal year + 1 >> ITERATION 2 | |
df.nbl.win.4 = | |
df.nbl.win.3 %>% | |
# rename cols for pmap | |
rename(year1=i.search.year) %>% #new.name=old.name | |
rename(i.search.year=year2) | |
df.nbl.win.4$nbl.yr1 = | |
df.nbl.win.4 %>% | |
select(c.country,i.search.year) %>% | |
pmap(fn_get_rank) %>% | |
unlist() | |
# mapping medal year + 2 >> ITERATION 3 | |
df.nbl.win.4 = | |
df.nbl.win.4 %>% | |
# rename cols for pmap | |
rename(year2=i.search.year) %>% | |
rename(i.search.year=year3) | |
df.nbl.win.4$nbl.yr2 = | |
df.nbl.win.4 %>% | |
select(c.country,i.search.year) %>% | |
pmap(fn_get_rank) %>% | |
unlist() | |
df.nbl.win.4 = | |
df.nbl.win.4 %>% | |
# rename cols for pmap | |
rename(year3=i.search.year) | |
df.nbl.win.4 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment