Skip to content

Instantly share code, notes, and snippets.

@njtierney
Created May 27, 2020 03:36
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 njtierney/22891827d08609843e3732672dac90b2 to your computer and use it in GitHub Desktop.
Save njtierney/22891827d08609843e3732672dac90b2 to your computer and use it in GitHub Desktop.
library(tidyverse)
chunk <- function(x, n){
  split(x, sort(x %% n))
}

special_chunk <- function(x, n){
  chunk(x, n) %>% 
    map(paste0, collapse = ", ") %>% 
    as.character()
}

special_chunk(1:6, 3)
#> [1] "1, 2" "3, 4" "5, 6"
aria <- tibble(rank = 1:50)

vec <- 1:150

aria %>% 
  mutate(extra_info = special_chunk(vec, 50)) %>% 
  separate(col = extra_info,
           into = c("total_weeks",
                    "current",
                    "top_number"))
#> # A tibble: 50 x 4
#>     rank total_weeks current top_number
#>    <int> <chr>       <chr>   <chr>     
#>  1     1 1           2       3         
#>  2     2 4           5       6         
#>  3     3 7           8       9         
#>  4     4 10          11      12        
#>  5     5 13          14      15        
#>  6     6 16          17      18        
#>  7     7 19          20      21        
#>  8     8 22          23      24        
#>  9     9 25          26      27        
#> 10    10 28          29      30        
#> # … with 40 more rows

Created on 2020-05-27 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment