String created by the combination of other strings

Hi,

I have three string vectors such as:

first <- c("apple", "banana", "orange")
second <- c("red", "green", "blue")
third <- c("small", "medium", "large")

I'd like to create all possible combinations of their componsents so that I would get

'apple_red_small', 'apple_red_medium', ..., 'orange_blue_medium', 'orange_blue_large'.

Many thanks for any tips!

Jakub

This is a possible tidyverse solution:

library(tidyverse)

first <- c("apple", "banana", "orange")
second <- c("red", "green", "blue")
third <- c("small", "medium", "large")

tibble(
  first = list(first),
  second = list(second),
  third = list(third)) |>
  unnest(first) |>
  unnest(second) |>
  unnest(third) |>
  mutate(result = str_glue("{first}_{second}_{third}"))
#> # A tibble: 27 × 4
#>    first  second third  result            
#>    <chr>  <chr>  <chr>  <glue>            
#>  1 apple  red    small  apple_red_small   
#>  2 apple  red    medium apple_red_medium  
#>  3 apple  red    large  apple_red_large   
#>  4 apple  green  small  apple_green_small 
#>  5 apple  green  medium apple_green_medium
#>  6 apple  green  large  apple_green_large 
#>  7 apple  blue   small  apple_blue_small  
#>  8 apple  blue   medium apple_blue_medium 
#>  9 apple  blue   large  apple_blue_large  
#> 10 banana red    small  banana_red_small  
#> # … with 17 more rows

Created on 2022-04-21 by the reprex package (v2.0.1)

Is this of any help? You can do it manually or write a function that could take any number of vectors!

NB: there may be a more straightforward way, but this will work.

# vectors

first <- c("apple", "banana", "orange")
second <- c("red", "green", "blue")
third <- c("small", "medium", "large")

# manual

combos <- expand.grid(first, second, third)

combos$all = paste(combos$Var1, combos$Var2, combos$Var3, sep = "_")

combos$all
#>  [1] "apple_red_small"     "banana_red_small"    "orange_red_small"   
#>  [4] "apple_green_small"   "banana_green_small"  "orange_green_small" 
#>  [7] "apple_blue_small"    "banana_blue_small"   "orange_blue_small"  
#> [10] "apple_red_medium"    "banana_red_medium"   "orange_red_medium"  
#> [13] "apple_green_medium"  "banana_green_medium" "orange_green_medium"
#> [16] "apple_blue_medium"   "banana_blue_medium"  "orange_blue_medium" 
#> [19] "apple_red_large"     "banana_red_large"    "orange_red_large"   
#> [22] "apple_green_large"   "banana_green_large"  "orange_green_large" 
#> [25] "apple_blue_large"    "banana_blue_large"   "orange_blue_large"

# write a function?

combine_vectors <- function(...){
  
  combos <- tidyr::crossing(...)

  united <- tidyr::unite(combos, col = "all", names(combos))
    
  dplyr::pull(united)
    
}

combine_vectors(first, third)
#> [1] "apple_large"   "apple_medium"  "apple_small"   "banana_large" 
#> [5] "banana_medium" "banana_small"  "orange_large"  "orange_medium"
#> [9] "orange_small"
combine_vectors(first, second)
#> [1] "apple_blue"   "apple_green"  "apple_red"    "banana_blue"  "banana_green"
#> [6] "banana_red"   "orange_blue"  "orange_green" "orange_red"
combine_vectors(first, second, third)
#>  [1] "apple_blue_large"    "apple_blue_medium"   "apple_blue_small"   
#>  [4] "apple_green_large"   "apple_green_medium"  "apple_green_small"  
#>  [7] "apple_red_large"     "apple_red_medium"    "apple_red_small"    
#> [10] "banana_blue_large"   "banana_blue_medium"  "banana_blue_small"  
#> [13] "banana_green_large"  "banana_green_medium" "banana_green_small" 
#> [16] "banana_red_large"    "banana_red_medium"   "banana_red_small"   
#> [19] "orange_blue_large"   "orange_blue_medium"  "orange_blue_small"  
#> [22] "orange_green_large"  "orange_green_medium" "orange_green_small" 
#> [25] "orange_red_large"    "orange_red_medium"   "orange_red_small"

Created on 2022-04-21 by the reprex package (v2.0.1)

for (x in 1:3) {
  for (y in 1:3) {
    for (z in 1:3) {
      print(paste(first[x], second[y], third[z], sep = '_'))
    }
  }
}

try this:

expand_grid(first,second,third) %>% str_glue_data("{first}_{second}_{third}")

1 Like

Nice one :sunglasses:

thanks

thanks :grin:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.