Home > Mobile >  How do I do a conditional cross list by referencing a column
How do I do a conditional cross list by referencing a column

Time:10-22

Rather than a 1 to 50 combinations to all a,b,c,d as per exhibit 1. I know the limits of each character types as per exhibit 2. How do i programmatically create exhibit 3 without explicitly typing each out, but instead reference the limits column. as per exhibit 3

exhibit 1

library(tidyverse)

txt1 <- c('a','b','c','d')
txt2 <- 1:50
combine <- cross(list(txt1,txt2)) %>% map_chr(paste, sep = "/", collapse = "")
combine
[1] "a1"  "b1"  "c1"  "d1"  "a2"  "b2"  "c2"  "d2"  "a3"  "b3"  "c3"  "d3"  "a4"  "b4"  "c4"  "d4"  "a5"  "b5"  "c5"  "d5"  "a6"  "b6"  "c6"  "d6"  "a7"  "b7"  "c7"  "d7"  "a8"  "b8"  "c8"  "d8"  "a9"  "b9"  "c9" 
 [36] "d9"  "a10" "b10" "c10" "d10" "a11" "b11" "c11" "d11" "a12" "b12" "c12" "d12" "a13" "b13" "c13" "d13" "a14" "b14" "c14" "d14" "a15" "b15" "c15" "d15" "a16" "b16" "c16" "d16" "a17" "b17" "c17" "d17" "a18" "b18"
 [71] "c18" "d18" "a19" "b19" "c19" "d19" "a20" "b20" "c20" "d20" "a21" "b21" "c21" "d21" "a22" "b22" "c22" "d22" "a23" "b23" "c23" "d23" "a24" "b24" "c24" "d24" "a25" "b25" "c25" "d25" "a26" "b26" "c26" "d26" "a27"
[106] "b27" "c27" "d27" "a28" "b28" "c28" "d28" "a29" "b29" "c29" "d29" "a30" "b30" "c30" "d30" "a31" "b31" "c31" "d31" "a32" "b32" "c32" "d32" "a33" "b33" "c33" "d33" "a34" "b34" "c34" "d34" "a35" "b35" "c35" "d35"
[141] "a36" "b36" "c36" "d36" "a37" "b37" "c37" "d37" "a38" "b38" "c38" "d38" "a39" "b39" "c39" "d39" "a40" "b40" "c40" "d40" "a41" "b41" "c41" "d41" "a42" "b42" "c42" "d42" "a43" "b43" "c43" "d43" "a44" "b44" "c44"
[176] "d44" "a45" "b45" "c45" "d45" "a46" "b46" "c46" "d46" "a47" "b47" "c47" "d47" "a48" "b48" "c48" "d48" "a49" "b49" "c49" "d49" "a50" "b50" "c50" "d50"
> 

Exhibit 2

real_limits <- tibble(alpha = c('a','b','c','d'),limits = c(5,15,10,30))
# A tibble: 4 x 2
  alpha limits
  <chr>  <dbl>
1 a          5
2 b         15
3 c         10
4 d         30

Exhibit 3

combine_new <- 
  c(cross(list('a',1:5)) %>% map_chr(paste, sep = "/", collapse = ""),
    cross(list('b',1:15)) %>% map_chr(paste, sep = "/", collapse = ""),
    cross(list('c',1:10)) %>% map_chr(paste, sep = "/", collapse = ""),
    cross(list('d',1:30)) %>% map_chr(paste, sep = "/", collapse = ""))

> combine_new
 [1] "a1"  "a2"  "a3"  "a4"  "a5"  "b1"  "b2"  "b3"  "b4"  "b5"  "b6"  "b7"  "b8"  "b9"  "b10" "b11" "b12" "b13" "b14" "b15" "c1"  "c2"  "c3"  "c4"  "c5"  "c6"  "c7"  "c8"  "c9"  "c10" "d1"  "d2"  "d3"  "d4"  "d5" 
[36] "d6"  "d7"  "d8"  "d9"  "d10" "d11" "d12" "d13" "d14" "d15" "d16" "d17" "d18" "d19" "d20" "d21" "d22" "d23" "d24" "d25" "d26" "d27" "d28" "d29" "d30"

CodePudding user response:

If you have a dataframe with the limits, then you could just use map to make a new variable, then pull the data out of the column you created. I'm not sure you need to use cross here.

library(tidyverse)
real_limits <- tibble(alpha = c('a','b','c','d'),limits = c(5,15,10,30))

real_limits %>%
  mutate(combs = map2(alpha, limits, ~paste0(.x, 1:.y))) %>%
  pull(combs) %>%
  unlist
#>  [1] "a1"  "a2"  "a3"  "a4"  "a5"  "b1"  "b2"  "b3"  "b4"  "b5"  "b6"  "b7" 
#> [13] "b8"  "b9"  "b10" "b11" "b12" "b13" "b14" "b15" "c1"  "c2"  "c3"  "c4" 
#> [25] "c5"  "c6"  "c7"  "c8"  "c9"  "c10" "d1"  "d2"  "d3"  "d4"  "d5"  "d6" 
#> [37] "d7"  "d8"  "d9"  "d10" "d11" "d12" "d13" "d14" "d15" "d16" "d17" "d18"
#> [49] "d19" "d20" "d21" "d22" "d23" "d24" "d25" "d26" "d27" "d28" "d29" "d30"

CodePudding user response:

This may be done with sequence and summarise

library(dplyr)
library(stringr)
real_limits %>%
   summarise(combs = str_c(rep(alpha, limits), sequence(limits))) %>% 
   pull(combs)
 [1] "a1"  "a2"  "a3"  "a4"  "a5"  "b1"  "b2"  "b3"  "b4"  "b5"  "b6"  "b7"  "b8"  "b9"  "b10" "b11" "b12" "b13" "b14" "b15" "c1"  "c2" 
[23] "c3"  "c4"  "c5"  "c6"  "c7"  "c8"  "c9"  "c10" "d1"  "d2"  "d3"  "d4"  "d5"  "d6"  "d7"  "d8"  "d9"  "d10" "d11" "d12" "d13" "d14"
[45] "d15" "d16" "d17" "d18" "d19" "d20" "d21" "d22" "d23" "d24" "d25" "d26" "d27" "d28" "d29" "d30"
  • Related