0

Is there way to get all possible values of a vector in R

a = c("A","B", "C")
b = c(1,2,3)

Expected output

new_vector <- c("A1","A2","A3","B1","B2","B3","C1","C2","C3")
manu p
  • 952
  • 4
  • 10

2 Answers2

1

Here's a way

df <- expand.grid(a, b)
vec <- paste0(df$Var1, df$Var2)
vec
# [1] "A1" "B1" "C1" "A2" "B2" "C2" "A3" "B3" "C3"

Then you can sort it to get the order you're after

sort(vec)
# [1] "A1" "A2" "A3" "B1" "B2" "B3" "C1" "C2" "C3"
stevec
  • 41,291
  • 27
  • 223
  • 311
0

Get the outer product of the two vectors then concatenate the result:

outer(a, b, FUN = "paste0") |> c()
Mwavu
  • 1,826
  • 6
  • 14