• 0

# Create an R or Python function to generate sets based on the value of C

## Question

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 15 hours ago.

Improve this question

I need help creating an R or Python function that generates sets based on the value of C. Here's an example to illustrate what I'm looking for:

For C = 2, I have 2^2 sets, including the sets: Cl_atypique, Cl_1, Cl_2, Cl_incertains

For C = 3, I have 2^3 sets, including the sets: Cl_atypique, Cl_1, Cl_2, Cl_1_2, Cl_3, Cl_1_3, Cl_2_3, Cl_incertains

For C = 4, I have 2^4 sets, including the sets: Cl_atypique, Cl_1, Cl_2, Cl_1_2, Cl_3, Cl_1_3, Cl_2_3, Cl_1_2_3, Cl_4, Cl_1_4, Cl_2_4, Cl_1_2_4,Cl_3_4,Cl_1_3_4, Cl_2_3_4, Cl_incertains

I would like to create an R or Python function that takes the value of C as input and returns a vector containing the sets in the specified order. This vector will be used to name a table later on.

I have this function that works, but the problem is with the order of the sets.

```get_ensembles <- function(C) { ensembles <- c() ensembles <- c(ensembles, "Cl_atypique") for (i in 1:(C-1)) { subsets <- combn(1:C, i) for (j in 1:ncol(subsets)) { ensemble <- subsets[, j] ensembles <- c(ensembles, paste0("Cl_", paste0(ensemble, collapse = "_"))) } } ensembles <- c(ensembles, "Cl_incertains") return(ensembles) } get_ensembles(3)

get_ensembles(3)  "Cl_atypique" "Cl_1" "Cl_2" "Cl_3" "Cl_1_2"
 "Cl_1_3" "Cl_2_3" "Cl_incertains"```

As can be seen in the output, Cl_3 and Cl_1_2 are reversed. I can't find a solution

Thank you !

✔ Наши пользователи помогли вам решить проблему? 💸 Наградите их реакцией и поддержите наш форум оформив подписку!
💎 Покупая премиум вы получаете дополнительные возможности и тем самым поддерживаете нас в продолжении!

• Created

## Recommended Posts

• 0

in R you could do:

`get_ensembles <- function(C){ cbs <- function(x,v = x){ if(length(x)<2) v else cbs(x[-1], c(v, x, paste(v, x, sep = '_'))) } paste0('CL_', c('atypique', cbs(seq.int(C))[-2^C+1], 'incertains')) } get_ensembles(4)  "CL_atypique" "CL_1" "CL_2" "CL_1_2"  "CL_3" "CL_1_3" "CL_2_3" "CL_1_2_3"  "CL_4" "CL_1_4" "CL_2_4" "CL_1_2_4"  "CL_3_4" "CL_1_3_4" "CL_2_3_4" "CL_incertains"`

You could directly convert the code above into python:

`import numpy as np def get_ensembles(C): def cbs(x, v = np.array('1')): if x.size < 2 : return v return cbs(x[1:], np.r_[v, np.array(x), np.char.add(v, np.char.add('_',x))]) return np.r_[np.array('CL_atypique'), cbs(np.arange(1,C+1).astype(str))[:-1], np.array('CL_incertains')] get_ensembles(4) array(['CL_atypique', '1', '2', '1_2', '3', '1_3', '2_3', '1_2_3', '4', '1_4', '2_4', '1_2_4', '3_4', '1_3_4', '2_3_4', 'CL_incertains'], dtype='<U13')`

✔ Наши пользователи помогли вам решить проблему? 💸 Наградите их реакцией и поддержите наш форум оформив подписку!
💎 Покупая премиум вы получаете дополнительные возможности и тем самым поддерживаете нас в продолжении!

## Create an account

Register a new account