Skip to content

Instantly share code, notes, and snippets.

@bhive01
Created March 4, 2018 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhive01/a1e9eed5de695847e3fd364b12272994 to your computer and use it in GitHub Desktop.
Save bhive01/a1e9eed5de695847e3fd364b12272994 to your computer and use it in GitHub Desktop.
library(tidyverse)
#-----------------------------------------------------------------------------
# Some complicated function to calculate things.
#-----------------------------------------------------------------------------
complex_func <- function(df) {
cat("processing: ", first(df[['ID']]), "\n")
# 100 lines of code go here.
df %>% mutate(something_complex_to_calculate = row_number())
}
#-----------------------------------------------------------------------------
# Project One - ID labels are unique across entire project
#-----------------------------------------------------------------------------
project_one <- readr::read_csv(
'ID, value1, value2
1 , 1, 1
1 , 1, 2
2 , 2, 1
2 , 2, 2
3 , 3, 1
3 , 3, 2
')
#-----------------------------------------------------------------------------
# Project 1 processing
#-----------------------------------------------------------------------------
project_one %>%
group_by(ID) %>%
do(complex_func(.)) %>%
ungroup()
#-----------------------------------------------------------------------------
# Some complicated function to calculate things.
#-----------------------------------------------------------------------------
complex_func2 <- function(df, ID) {
cat("processing: ID =", ID, "\n")
# 100 lines of code go here.
df %>% mutate(something_complex_to_calculate = row_number())
}
project_one %>%
nest(-ID) %>%
mutate(data = map2(data, ID, ~ complex_func2(.x, .y))) %>%
unnest()
#-----------------------------------------------------------------------------
# Project Two. ID labels are only unique within GROUP
#-----------------------------------------------------------------------------
project_two <- readr::read_csv(
'ID, GROUP, value1, value2
1 , 1, 1, 1
1 , 1, 1, 2
2 , 1, 2, 1
2 , 1, 2, 2
1 , 2, 3, 1
1 , 2, 3, 2
')
#-----------------------------------------------------------------------------
# Project Two - processing.
#-----------------------------------------------------------------------------
project_two %>%
group_by(GROUP, ID) %>%
do(complex_func(.)) %>%
ungroup()
#-----------------------------------------------------------------------------
# Some complicated function to calculate things.
#-----------------------------------------------------------------------------
complex_func3 <- function(df, ID, GROUP) {
cat("processing: ID = ", ID, "GROUP = ", GROUP , "\n")
# 100 lines of code go here.
df %>% mutate(something_complex_to_calculate = row_number())
}
project_two %>%
nest(-ID, -GROUP) %>%
mutate(data = pmap(.l = list(data, ID, GROUP), .f = ~complex_func3(..1, ..2, ..3))) %>%
unnest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment