RDP RDP - 6 months ago 27
R Question

Create sublists based on names of lists in R

predictors_list = list("predictors_pred_cluster_pair1_1","predictors_pred_cluster_pair1_2","predictors_pred_cluster_pair2_1","predictors_pred_cluster_pair2_2","predictors_pred_cluster_pair2_3","predictors_pred_cluster_pair3_1","predictors_pred_cluster_pair3_2","predictors_pred_cluster_pair3_3","predictors_pred_cluster_pair3_4","predictors_pred_cluster_pair4_1","predictors_pred_cluster_pair4_2","predictors_pred_cluster_pair4_3","predictors_pred_cluster_pair4_4")
predictors_list <- unlist(predictors_list)
pair_list1 <- str_replace(predictors_list,"^predictors_pred_cluster_","")
pair_list <- unique(str_replace(pair_list1,"_[0-9]",""))

> pair_list
[1] "pair1" "pair2" "pair3" "pair4"

> pair_list1
[1] "pair1_1" "pair1_2" "pair2_1" "pair2_2" "pair2_3" "pair3_1" "pair3_2"
"pair3_3" "pair3_4" "pair4_1" "pair4_2" "pair4_3" "pair4_4"

I have above lists and now I am trying to create sublists for each pair.

Like- pair1 should have 2 sublists,
pair2 should have 3 sublists,
pair3 should have 4 sublists,
pair4 should have 4 sublists.

There could be any number of sublists. This is just a sample. Please help.

Desired output-

pair1 <- list("pair1_1", "pair1_2")
pair2 <- list("pair2_1", "pair2_2","pair2_3")
pair3 <- list("pair3_1", "pair3_2","pair3_3","pair3_4")
pair4 <- list("pair4_1", "pair4_2","pair4_3","pair4_4")
pair_list <- list(pair1=pair1,pair2=pair2,pair3=pair3,pair4=pair4)

[1] "pair1_1"

[1] "pair1_2"

[1] "pair2_1"

[1] "pair2_2"

[1] "pair2_3"

[1] "pair3_1"

[1] "pair3_2"

[1] "pair3_3"

[1] "pair3_4"

[1] "pair4_1"

[1] "pair4_2"

[1] "pair4_3"

[1] "pair4_4"

I want above but i dont know what could be exact pair number( it could be pair1...pairn). Similarly, I don't know what could be pair1_1 (pair1_1...pair1_n). Please let me know how I can do it.

Answer Source

Try this one out:


If it helps, let us know. Thank you

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download