Home > Software engineering >  Is there a function to concatenate all the elements in a vector?
Is there a function to concatenate all the elements in a vector?

Time:10-20

I have made a vector which looks like this:

v1 <- c("1 1","1 2","1 3",
        "2 1","2 2","2 3",
        "3 1","3 2","3 3",
        "4 1","4 2","4 3",
        "5 1","5 2","5 3",
        "6 1","6 2","6 3")

It can be called v1.

The result I want is "1 1 1 1" "1 1 1 2" .... "6 6 6 6" (in total should be 6x3x6x3=360-36=324 in total into a new vector v2)

However, I have tried apply(combn(v1, 2), 2, paste0, collapse=" ") but it is not complete.

How can I achieve the goal?

CodePudding user response:

Get all combinations, then paste:

v2 <- do.call(paste, expand.grid(v1, v1))

Check the output:

head(v2)
# [1] "1 1 1 1" "1 2 1 1" "1 3 1 1" "2 1 1 1" "2 2 1 1" "2 3 1 1"
tail(v2)
# [1] "5 1 6 3" "5 2 6 3" "5 3 6 3" "6 1 6 3" "6 2 6 3" "6 3 6 3"    
length(v2)
# [1] 324

CodePudding user response:

You can try outer paste to make it

> (v2 <- c(outer(v1, v1, paste)))
  [1] "1 1 1 1" "1 2 1 1" "1 3 1 1" "2 1 1 1" "2 2 1 1" "2 3 1 1" "3 1 1 1"
  [8] "3 2 1 1" "3 3 1 1" "4 1 1 1" "4 2 1 1" "4 3 1 1" "5 1 1 1" "5 2 1 1"
 [15] "5 3 1 1" "6 1 1 1" "6 2 1 1" "6 3 1 1" "1 1 1 2" "1 2 1 2" "1 3 1 2"
 [22] "2 1 1 2" "2 2 1 2" "2 3 1 2" "3 1 1 2" "3 2 1 2" "3 3 1 2" "4 1 1 2"
 [29] "4 2 1 2" "4 3 1 2" "5 1 1 2" "5 2 1 2" "5 3 1 2" "6 1 1 2" "6 2 1 2"
 [36] "6 3 1 2" "1 1 1 3" "1 2 1 3" "1 3 1 3" "2 1 1 3" "2 2 1 3" "2 3 1 3"
 [43] "3 1 1 3" "3 2 1 3" "3 3 1 3" "4 1 1 3" "4 2 1 3" "4 3 1 3" "5 1 1 3"
 [50] "5 2 1 3" "5 3 1 3" "6 1 1 3" "6 2 1 3" "6 3 1 3" "1 1 2 1" "1 2 2 1"
 [57] "1 3 2 1" "2 1 2 1" "2 2 2 1" "2 3 2 1" "3 1 2 1" "3 2 2 1" "3 3 2 1"
 [64] "4 1 2 1" "4 2 2 1" "4 3 2 1" "5 1 2 1" "5 2 2 1" "5 3 2 1" "6 1 2 1"
 [71] "6 2 2 1" "6 3 2 1" "1 1 2 2" "1 2 2 2" "1 3 2 2" "2 1 2 2" "2 2 2 2"
 [78] "2 3 2 2" "3 1 2 2" "3 2 2 2" "3 3 2 2" "4 1 2 2" "4 2 2 2" "4 3 2 2"
 [85] "5 1 2 2" "5 2 2 2" "5 3 2 2" "6 1 2 2" "6 2 2 2" "6 3 2 2" "1 1 2 3"
 [92] "1 2 2 3" "1 3 2 3" "2 1 2 3" "2 2 2 3" "2 3 2 3" "3 1 2 3" "3 2 2 3"
 [99] "3 3 2 3" "4 1 2 3" "4 2 2 3" "4 3 2 3" "5 1 2 3" "5 2 2 3" "5 3 2 3"
[106] "6 1 2 3" "6 2 2 3" "6 3 2 3" "1 1 3 1" "1 2 3 1" "1 3 3 1" "2 1 3 1"
[113] "2 2 3 1" "2 3 3 1" "3 1 3 1" "3 2 3 1" "3 3 3 1" "4 1 3 1" "4 2 3 1"
[120] "4 3 3 1" "5 1 3 1" "5 2 3 1" "5 3 3 1" "6 1 3 1" "6 2 3 1" "6 3 3 1"
[127] "1 1 3 2" "1 2 3 2" "1 3 3 2" "2 1 3 2" "2 2 3 2" "2 3 3 2" "3 1 3 2"
[134] "3 2 3 2" "3 3 3 2" "4 1 3 2" "4 2 3 2" "4 3 3 2" "5 1 3 2" "5 2 3 2"
[141] "5 3 3 2" "6 1 3 2" "6 2 3 2" "6 3 3 2" "1 1 3 3" "1 2 3 3" "1 3 3 3"
[148] "2 1 3 3" "2 2 3 3" "2 3 3 3" "3 1 3 3" "3 2 3 3" "3 3 3 3" "4 1 3 3"
[155] "4 2 3 3" "4 3 3 3" "5 1 3 3" "5 2 3 3" "5 3 3 3" "6 1 3 3" "6 2 3 3"
[162] "6 3 3 3" "1 1 4 1" "1 2 4 1" "1 3 4 1" "2 1 4 1" "2 2 4 1" "2 3 4 1"
[169] "3 1 4 1" "3 2 4 1" "3 3 4 1" "4 1 4 1" "4 2 4 1" "4 3 4 1" "5 1 4 1"
[176] "5 2 4 1" "5 3 4 1" "6 1 4 1" "6 2 4 1" "6 3 4 1" "1 1 4 2" "1 2 4 2"
[183] "1 3 4 2" "2 1 4 2" "2 2 4 2" "2 3 4 2" "3 1 4 2" "3 2 4 2" "3 3 4 2"
[190] "4 1 4 2" "4 2 4 2" "4 3 4 2" "5 1 4 2" "5 2 4 2" "5 3 4 2" "6 1 4 2"
[197] "6 2 4 2" "6 3 4 2" "1 1 4 3" "1 2 4 3" "1 3 4 3" "2 1 4 3" "2 2 4 3"
[204] "2 3 4 3" "3 1 4 3" "3 2 4 3" "3 3 4 3" "4 1 4 3" "4 2 4 3" "4 3 4 3"
[211] "5 1 4 3" "5 2 4 3" "5 3 4 3" "6 1 4 3" "6 2 4 3" "6 3 4 3" "1 1 5 1"
[218] "1 2 5 1" "1 3 5 1" "2 1 5 1" "2 2 5 1" "2 3 5 1" "3 1 5 1" "3 2 5 1"
[225] "3 3 5 1" "4 1 5 1" "4 2 5 1" "4 3 5 1" "5 1 5 1" "5 2 5 1" "5 3 5 1"
[232] "6 1 5 1" "6 2 5 1" "6 3 5 1" "1 1 5 2" "1 2 5 2" "1 3 5 2" "2 1 5 2"
[239] "2 2 5 2" "2 3 5 2" "3 1 5 2" "3 2 5 2" "3 3 5 2" "4 1 5 2" "4 2 5 2"
[246] "4 3 5 2" "5 1 5 2" "5 2 5 2" "5 3 5 2" "6 1 5 2" "6 2 5 2" "6 3 5 2"
[253] "1 1 5 3" "1 2 5 3" "1 3 5 3" "2 1 5 3" "2 2 5 3" "2 3 5 3" "3 1 5 3"
[260] "3 2 5 3" "3 3 5 3" "4 1 5 3" "4 2 5 3" "4 3 5 3" "5 1 5 3" "5 2 5 3"
[267] "5 3 5 3" "6 1 5 3" "6 2 5 3" "6 3 5 3" "1 1 6 1" "1 2 6 1" "1 3 6 1"
[274] "2 1 6 1" "2 2 6 1" "2 3 6 1" "3 1 6 1" "3 2 6 1" "3 3 6 1" "4 1 6 1"
[281] "4 2 6 1" "4 3 6 1" "5 1 6 1" "5 2 6 1" "5 3 6 1" "6 1 6 1" "6 2 6 1"
[288] "6 3 6 1" "1 1 6 2" "1 2 6 2" "1 3 6 2" "2 1 6 2" "2 2 6 2" "2 3 6 2"
[295] "3 1 6 2" "3 2 6 2" "3 3 6 2" "4 1 6 2" "4 2 6 2" "4 3 6 2" "5 1 6 2"
[302] "5 2 6 2" "5 3 6 2" "6 1 6 2" "6 2 6 2" "6 3 6 2" "1 1 6 3" "1 2 6 3"
[309] "1 3 6 3" "2 1 6 3" "2 2 6 3" "2 3 6 3" "3 1 6 3" "3 2 6 3" "3 3 6 3"
[316] "4 1 6 3" "4 2 6 3" "4 3 6 3" "5 1 6 3" "5 2 6 3" "5 3 6 3" "6 1 6 3"
[323] "6 2 6 3" "6 3 6 3"

CodePudding user response:

A compact solution with levels interaction:

levels(interaction(v1, v1, sep = " "))
head(v)
# [1] "1 1 1 1" "1 2 1 1" "1 3 1 1" "2 1 1 1" "2 2 1 1" "2 3 1 1"
tail(v)
# [1] "5 1 6 3" "5 2 6 3" "5 3 6 3" "6 1 6 3" "6 2 6 3" "6 3 6 3"    
length(v)
# [1] 324

Previous answer, with pmap_chr expand.grid:

library(purrr)
v <- pmap_chr(expand.grid(v1, v1), paste)

length(v)
#[1] 324

CodePudding user response:

Not sure if this is what you are after but expand.grid might be useful:

library(dplyr)

expand.grid(v1,v1) %>% 
  data.frame() %>% 
  mutate(v2 = paste0(Var2," ",Var1)) %>% 
  pull(v2)

output:

 [1] "1 1 1 1" "1 1 1 2" "1 1 1 3" "1 1 2 1" ... "6 3 6 1" "6 3 6 2" "6 3 6 3"

CodePudding user response:

Here is a one-liner using RcppAlgos (I am the author):

library(RcppAlgos)
permuteGeneral(v1, 2, TRUE, FUN = \(x) paste(x, collapse = " "), FUN.VALUE = "1")
#>   [1] "1 1 1 1" "1 1 1 2" "1 1 1 3" "1 1 2 1" "1 1 2 2" "1 1 2 3"
#>   [7] "1 1 3 1" "1 1 3 2" "1 1 3 3" "1 1 4 1" "1 1 4 2" "1 1 4 3"
#>     .
#>     .
#>     .
#> [313] "6 3 3 1" "6 3 3 2" "6 3 3 3" "6 3 4 1" "6 3 4 2" "6 3 4 3"
#> [319] "6 3 5 1" "6 3 5 2" "6 3 5 3" "6 3 6 1" "6 3 6 2" "6 3 6 3"

This extends easily to permutations of greater length as well as without repetition (i.e. altering the value of m and the value of repetition).

N.B. If you want a list instead, simply leave FUN.VALUE as NULL.

  • Related