Home > Software design >  Combine multiple paste0 to one paste0
Combine multiple paste0 to one paste0

Time:01-08

I am creating vectors of different length using paste0() and concatenating to one vector:

c(
paste0("A_", 1:16),
paste0("B_", 1:12),
paste0("C_", 3:6),
paste0("D_3_4_C_1_2"),
paste0("E_", rep(1:2, 4), "_", rep(1:4, each=2))
)
 [1] "A_1"         "A_2"         "A_3"         "A_4"         "A_5"         "A_6"         "A_7"         "A_8"        
 [9] "A_9"         "A_10"        "A_11"        "A_12"        "A_13"        "A_14"        "A_15"        "A_16"       
[17] "B_1"         "B_2"         "B_3"         "B_4"         "B_5"         "B_6"         "B_7"         "B_8"        
[25] "B_9"         "B_10"        "B_11"        "B_12"        "C_3"         "C_4"         "C_5"         "C_6"        
[33] "D_3_4_C_1_2" "E_1_1"       "E_2_1"       "E_1_2"       "E_2_2"       "E_1_3"       "E_2_3"       "E_1_4"      
[41] "E_2_4"      

My question is: In a scenario where multiple vectors should be created, is it possible to combine all the paste0 to one paste0:

Desired output: Something like paste0(("A_", 1:16), ("B_", 1:12)) -> which does not work!!!

update: Desired output Something like this non working code: Note I removed 4 paste0()

c(
paste0("A_", 1:16),
      ("B_", 1:12),
      ("C_", 3:6),
      ("D_3_4_C_1_2"),
      ("E_", rep(1:2, 4), "_", rep(1:4, each=2))
)

CodePudding user response:

Maybe this is an option by creating two lists with the first list of the strings and the second lists of the number and combining them with a paste0 with mapply like this:

l1 <- list("A_", "B_", "C_", "D_3_4_C_1_2")
l2 <- list(1:16, 1:12, 3:6, 1)
unlist(mapply(paste0, l1, l2))
#>  [1] "A_1"          "A_2"          "A_3"          "A_4"          "A_5"         
#>  [6] "A_6"          "A_7"          "A_8"          "A_9"          "A_10"        
#> [11] "A_11"         "A_12"         "A_13"         "A_14"         "A_15"        
#> [16] "A_16"         "B_1"          "B_2"          "B_3"          "B_4"         
#> [21] "B_5"          "B_6"          "B_7"          "B_8"          "B_9"         
#> [26] "B_10"         "B_11"         "B_12"         "C_3"          "C_4"         
#> [31] "C_5"          "C_6"          "D_3_4_C_1_21"

Created on 2023-01-08 with reprex v2.0.2

CodePudding user response:

Is string_flatten what you are looking for?

stringr::str_flatten_comma(c(paste0("A_", 1:16), paste0("B_", 1:12)))

#> [1] "A_1, A_2, A_3, A_4, A_5, A_6, A_7, A_8, A_9, A_10, A_11, A_12, A_13, A_14, A_15, A_16, B_1, B_2, B_3, B_4, B_5, B_6, B_7, B_8, B_9, B_10, B_11, B_12"

stringr::str_flatten(c(paste0("A_", 1:16), paste0("B_", 1:12)))

#> [1] "A_1A_2A_3A_4A_5A_6A_7A_8A_9A_10A_11A_12A_13A_14A_15A_16B_1B_2B_3B_4B_5B_6B_7B_8B_9B_10B_11B_12"

New Edit

Taking the two lists from @Quinten and your paste0("E_", rep(1:2, 4), "_", rep(1:4, each=2)) from the comment above, I would like to offer the following 2 ways with purrr:

l1 <- list("A_", "B_", "C_", "D_3_4_C_1_2")
l2 <- list(1:16, 1:12, 3:6, 1)


purrr::map2(l1, l2, paste0) |> unlist() |> stringr::str_flatten_comma()
#> [1] "A_1, A_2, A_3, A_4, A_5, A_6, A_7, A_8, A_9, A_10, A_11, A_12, A_13, A_14, A_15, A_16, B_1, B_2, B_3, B_4, B_5, B_6, B_7, B_8, B_9, B_10, B_11, B_12, C_3, C_4, C_5, C_6, D_3_4_C_1_21"
 
# this is from your comment
l3 <- list("E_", rep(1:2, 4))
l32 <- purrr::map2(l3[[1]], l3[[2]], paste0, '_')
l4 <- list(rep(1:4, each=2))

purrr::map2(l32, l4, paste0) |> unlist() |> stringr::str_flatten_comma()
#> [1] "E_1_1, E_1_1, E_1_2, E_1_2, E_1_3, E_1_3, E_1_4, E_1_4, E_2_1, E_2_1, E_2_2, E_2_2, E_2_3, E_2_3, E_2_4, E_2_4, E_1_1, E_1_1, E_1_2, E_1_2, E_1_3, E_1_3, E_1_4, E_1_4, E_2_1, E_2_1, E_2_2, E_2_2, E_2_3, E_2_3, E_2_4, E_2_4, E_1_1, E_1_1, E_1_2, E_1_2, E_1_3, E_1_3, E_1_4, E_1_4, E_2_1, E_2_1, E_2_2, E_2_2, E_2_3, E_2_3, E_2_4, E_2_4, E_1_1, E_1_1, E_1_2, E_1_2, E_1_3, E_1_3, E_1_4, E_1_4, E_2_1, E_2_1, E_2_2, E_2_2, E_2_3, E_2_3, E_2_4, E_2_4"


purrr::map2(l32, l4, paste0) |> unlist()
#>  [1] "E_1_1" "E_1_1" "E_1_2" "E_1_2" "E_1_3" "E_1_3" "E_1_4" "E_1_4" "E_2_1"
#> [10] "E_2_1" "E_2_2" "E_2_2" "E_2_3" "E_2_3" "E_2_4" "E_2_4" "E_1_1" "E_1_1"
#> [19] "E_1_2" "E_1_2" "E_1_3" "E_1_3" "E_1_4" "E_1_4" "E_2_1" "E_2_1" "E_2_2"
#> [28] "E_2_2" "E_2_3" "E_2_3" "E_2_4" "E_2_4" "E_1_1" "E_1_1" "E_1_2" "E_1_2"
#> [37] "E_1_3" "E_1_3" "E_1_4" "E_1_4" "E_2_1" "E_2_1" "E_2_2" "E_2_2" "E_2_3"
#> [46] "E_2_3" "E_2_4" "E_2_4" "E_1_1" "E_1_1" "E_1_2" "E_1_2" "E_1_3" "E_1_3"
#> [55] "E_1_4" "E_1_4" "E_2_1" "E_2_1" "E_2_2" "E_2_2" "E_2_3" "E_2_3" "E_2_4"
#> [64] "E_2_4"
  • Related