Home > Software engineering >  Can I combine the output from two vectors using a for-loop, so that the nth element is paired with t
Can I combine the output from two vectors using a for-loop, so that the nth element is paired with t

Time:11-24

I'm trying to automate some tedious code-writing. I have something like the following:

Codes<-c("code1","code2","code3","code4")
other_Codes<-c("code5","code6","code7","code8")

What I want to create is something like the following:

repetetivetext Code1 repetetivetext Code5 
repetetivetext Code2 repetetivetext Code6 
repetetivetext Code3 repetetivetext Code7
repetetivetext Code4 repetetivetext Code8

....So that the first argument in the first vector is paired with the first argument in the second vector and so on. This can be done with something like:

paste0("repetetivetext ",Codes, "repetitive text ", other_Codes)

But for different reason (the actual code is more complex than this) this isn't a workable solution right now. I would much rather use a variation of a for loop or nested for-loop, one which would let me combine the elements from the two vectors, but give me 4 combinations instead of 16.

Is there such a variation? Or is there a different way of doing this which I haven't thought of?

CodePudding user response:

you can use just 1 iteration of 'for loop', perhaps you look for something like this? :

Codes <- c("code1", "code2", "code3", "code4")
other_Codes <- c("code5", "code6", "code7", "code8")
output = c()
text = "repetetivetext "
for (j in 1:length(Codes)) { 
    element = paste0(text, Codes[j], text,other_Codes[j])
    output  = c(output, element)
}
output

CodePudding user response:

If you want to combine the two vectors both of which consist of four elements to get all 16 combinations, you have to multiply the elements of a vector according to the length of the other vector. You can do that in a (nested) for-loop using purrr::map2 and its family (e.g. purrr::map2_chr).

library(purrr)

Codes <- c("code1", "code2", "code3", "code4")
other_Codes <- c("code5", "code6", "code7", "code8")

map2_chr(
  .x = rep(Codes, each = length(other_Codes)),
  .y = rep(other_Codes, times = length(Codes)),
  ~ paste0("repetetive text ", .x, " repetitive text ", .y)
)

#  [1] "repetetive text code1 repetitive text code5"
#  [2] "repetetive text code1 repetitive text code6"
#  [3] "repetetive text code1 repetitive text code7"
#  [4] "repetetive text code1 repetitive text code8"
#  [5] "repetetive text code2 repetitive text code5"
#  [6] "repetetive text code2 repetitive text code6"
#  [7] "repetetive text code2 repetitive text code7"
#  [8] "repetetive text code2 repetitive text code8"
#  [9] "repetetive text code3 repetitive text code5"
# [10] "repetetive text code3 repetitive text code6"
# [11] "repetetive text code3 repetitive text code7"
# [12] "repetetive text code3 repetitive text code8"
# [13] "repetetive text code4 repetitive text code5"
# [14] "repetetive text code4 repetitive text code6"
# [15] "repetetive text code4 repetitive text code7"
# [16] "repetetive text code4 repetitive text code8"
  •  Tags:  
  • r
  • Related