List of character combinations

I am trying to take a vector with the names of the four possible models "x1", "x2", "x3", "x4" and create a longer vector of all possible permutations of these terms, so I can start a dataframe with the properties of each.

The code below seems to work, but it doesn’t work. Elements are not inserted together, for example, the combination of "x1" and "x2" should result in the model name "x1x2", but instead it remains "x1" "x2".

models <- c("x1","x2","x3","x4")

modelist<- as.character()
for(i in 1:4){
  modelist <- c(modelist,combn(models,m=i,FUN=paste0,simplify = T))
}
modelist

Since I am not looking for interaction conditions, x1x1 should not appear, but I would be interested to know how to do this for future use.

Here is the output I'm looking for:

modelist <- c("x1","x2","x3","x4","x1x2","x1x3","x1x4","x2x3","x2x4","x3x4",
            "x1x2x3","x2x3x4","x1x2x4","x1x3x4","x1x2x3x4")

What should be contained in the model.

+4
1

paste() , sep = "" , combn() , . , , .

paste(letters[1:4])
# [1] "a" "b" "c" "d"
paste(letters[1:4], sep = "")
# [1] "a" "b" "c" "d"
paste(letters[1:4], collapse = "")
# [1] "abcd"

, collapse = "" paste(). lapply() .

unlist(lapply(1:4, function(i) combn(models, i, paste, collapse = "")))
#  [1] "x1"       "x2"       "x3"       "x4"       "x1x2"     "x1x3"     "x1x4"
#  [8] "x2x3"     "x2x4"     "x3x4"     "x1x2x3"   "x1x2x4"   "x1x3x4"   "x2x3x4"
# [15] "x1x2x3x4"

for(), . . sum(choose(4, 1:4)), , , . , , .

modlist <- vector("list", 4)
for(i in 1:4) {
    modlist[[i]] <- combn(models, i, paste, collapse = "")
}
unlist(modlist)
#  [1] "x1"       "x2"       "x3"       "x4"       "x1x2"     "x1x3"     "x1x4"
#  [8] "x2x3"     "x2x4"     "x3x4"     "x1x2x3"   "x1x2x4"   "x1x3x4"   "x2x3x4"
# [15] "x1x2x3x4"
+5

Source: https://habr.com/ru/post/1614081/


All Articles