How to splitting a list of vectors to small lists in decreasing order in r










2















Suppose I have a function which returns me a list of several vectors. Assume that I would like to split this list to small lists of different numbers of vectors. The number of the vectors in each list is different from one list to another. I need to create these lists in a decreasing order.



Genearal example:



Suppose I have n variables. Then, k = n:2. My function will return me a list of n(n-1)/2 vectors. Then, the number of the new sub-lists is n - 1. Hence, I need to have n-1 different lists. The number of the vectors in each list is J = 1:k-1.



Numerical example:



If n=4, then, k=4, 3, 2, then, J=3,2,1. Hence, my function will return me 6 vectors. These vectors should be stored into different lists. The number of the vectors in each list is based on J. Hence, I will have 3 different lists as follows:



  • 3 vectors in the first list.

  • 2 vectors in the second list.

  • one vector in the last list.

In other words, the returned list (the output of my function) should be split into sub-lists in a decreasing order based on J.



My function is very complicated. Hence, I will provide a list of 6 vectors as the output of my function.



Suppose my function return me the following list:



x <- list(x1=c(1,2,3), x2=c(1,4,3), x3=c(3,4,6), x4=c(4,8,4), x5=c(4,33,4), x6=c(9,6,7))


How I can split it into sub-lists as described above? The excepted output is:



x_sub1 <- list(x1=c(1,2,3), x2=c(1,4,3), x3=c(3,4,6))
x_sub2 <- list(x4=c(4,8,4), x5=c(4,33,4))
x_sub3 <- list(x6=c(9,6,7))


I tried this:



x_sub <- list()
for(j in 1:(k-1))
x_sub[[j]] <- x[[i]]



and of course, it is not what I expected.



Any idea, please? How I generate it for an arbitrary number of vectors? for example, how I can apply the split idea over n vectors?



Many thanks for all helps.










share|improve this question
























  • Sorry, it is not clear to me

    – akrun
    Nov 11 '18 at 5:45











  • @MrFlick, thank you so much. This is a typo. I will edit it.

    – Maryam
    Nov 11 '18 at 5:48











  • @akrun Thank you so much for your comment and answer. I just meant how I can generate it to the n number of vectors. Then, I found that you delete your answer. I am really so sorry if I misunderstand you.

    – Maryam
    Nov 12 '18 at 4:48






  • 1





    @akrun I am always proud of your work. It is correct. I really surprised why someone downvoted it.

    – Maryam
    Nov 13 '18 at 9:28















2















Suppose I have a function which returns me a list of several vectors. Assume that I would like to split this list to small lists of different numbers of vectors. The number of the vectors in each list is different from one list to another. I need to create these lists in a decreasing order.



Genearal example:



Suppose I have n variables. Then, k = n:2. My function will return me a list of n(n-1)/2 vectors. Then, the number of the new sub-lists is n - 1. Hence, I need to have n-1 different lists. The number of the vectors in each list is J = 1:k-1.



Numerical example:



If n=4, then, k=4, 3, 2, then, J=3,2,1. Hence, my function will return me 6 vectors. These vectors should be stored into different lists. The number of the vectors in each list is based on J. Hence, I will have 3 different lists as follows:



  • 3 vectors in the first list.

  • 2 vectors in the second list.

  • one vector in the last list.

In other words, the returned list (the output of my function) should be split into sub-lists in a decreasing order based on J.



My function is very complicated. Hence, I will provide a list of 6 vectors as the output of my function.



Suppose my function return me the following list:



x <- list(x1=c(1,2,3), x2=c(1,4,3), x3=c(3,4,6), x4=c(4,8,4), x5=c(4,33,4), x6=c(9,6,7))


How I can split it into sub-lists as described above? The excepted output is:



x_sub1 <- list(x1=c(1,2,3), x2=c(1,4,3), x3=c(3,4,6))
x_sub2 <- list(x4=c(4,8,4), x5=c(4,33,4))
x_sub3 <- list(x6=c(9,6,7))


I tried this:



x_sub <- list()
for(j in 1:(k-1))
x_sub[[j]] <- x[[i]]



and of course, it is not what I expected.



Any idea, please? How I generate it for an arbitrary number of vectors? for example, how I can apply the split idea over n vectors?



Many thanks for all helps.










share|improve this question
























  • Sorry, it is not clear to me

    – akrun
    Nov 11 '18 at 5:45











  • @MrFlick, thank you so much. This is a typo. I will edit it.

    – Maryam
    Nov 11 '18 at 5:48











  • @akrun Thank you so much for your comment and answer. I just meant how I can generate it to the n number of vectors. Then, I found that you delete your answer. I am really so sorry if I misunderstand you.

    – Maryam
    Nov 12 '18 at 4:48






  • 1





    @akrun I am always proud of your work. It is correct. I really surprised why someone downvoted it.

    – Maryam
    Nov 13 '18 at 9:28













2












2








2








Suppose I have a function which returns me a list of several vectors. Assume that I would like to split this list to small lists of different numbers of vectors. The number of the vectors in each list is different from one list to another. I need to create these lists in a decreasing order.



Genearal example:



Suppose I have n variables. Then, k = n:2. My function will return me a list of n(n-1)/2 vectors. Then, the number of the new sub-lists is n - 1. Hence, I need to have n-1 different lists. The number of the vectors in each list is J = 1:k-1.



Numerical example:



If n=4, then, k=4, 3, 2, then, J=3,2,1. Hence, my function will return me 6 vectors. These vectors should be stored into different lists. The number of the vectors in each list is based on J. Hence, I will have 3 different lists as follows:



  • 3 vectors in the first list.

  • 2 vectors in the second list.

  • one vector in the last list.

In other words, the returned list (the output of my function) should be split into sub-lists in a decreasing order based on J.



My function is very complicated. Hence, I will provide a list of 6 vectors as the output of my function.



Suppose my function return me the following list:



x <- list(x1=c(1,2,3), x2=c(1,4,3), x3=c(3,4,6), x4=c(4,8,4), x5=c(4,33,4), x6=c(9,6,7))


How I can split it into sub-lists as described above? The excepted output is:



x_sub1 <- list(x1=c(1,2,3), x2=c(1,4,3), x3=c(3,4,6))
x_sub2 <- list(x4=c(4,8,4), x5=c(4,33,4))
x_sub3 <- list(x6=c(9,6,7))


I tried this:



x_sub <- list()
for(j in 1:(k-1))
x_sub[[j]] <- x[[i]]



and of course, it is not what I expected.



Any idea, please? How I generate it for an arbitrary number of vectors? for example, how I can apply the split idea over n vectors?



Many thanks for all helps.










share|improve this question
















Suppose I have a function which returns me a list of several vectors. Assume that I would like to split this list to small lists of different numbers of vectors. The number of the vectors in each list is different from one list to another. I need to create these lists in a decreasing order.



Genearal example:



Suppose I have n variables. Then, k = n:2. My function will return me a list of n(n-1)/2 vectors. Then, the number of the new sub-lists is n - 1. Hence, I need to have n-1 different lists. The number of the vectors in each list is J = 1:k-1.



Numerical example:



If n=4, then, k=4, 3, 2, then, J=3,2,1. Hence, my function will return me 6 vectors. These vectors should be stored into different lists. The number of the vectors in each list is based on J. Hence, I will have 3 different lists as follows:



  • 3 vectors in the first list.

  • 2 vectors in the second list.

  • one vector in the last list.

In other words, the returned list (the output of my function) should be split into sub-lists in a decreasing order based on J.



My function is very complicated. Hence, I will provide a list of 6 vectors as the output of my function.



Suppose my function return me the following list:



x <- list(x1=c(1,2,3), x2=c(1,4,3), x3=c(3,4,6), x4=c(4,8,4), x5=c(4,33,4), x6=c(9,6,7))


How I can split it into sub-lists as described above? The excepted output is:



x_sub1 <- list(x1=c(1,2,3), x2=c(1,4,3), x3=c(3,4,6))
x_sub2 <- list(x4=c(4,8,4), x5=c(4,33,4))
x_sub3 <- list(x6=c(9,6,7))


I tried this:



x_sub <- list()
for(j in 1:(k-1))
x_sub[[j]] <- x[[i]]



and of course, it is not what I expected.



Any idea, please? How I generate it for an arbitrary number of vectors? for example, how I can apply the split idea over n vectors?



Many thanks for all helps.







r






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 '18 at 5:47







Maryam

















asked Nov 11 '18 at 5:33









MaryamMaryam

18211




18211












  • Sorry, it is not clear to me

    – akrun
    Nov 11 '18 at 5:45











  • @MrFlick, thank you so much. This is a typo. I will edit it.

    – Maryam
    Nov 11 '18 at 5:48











  • @akrun Thank you so much for your comment and answer. I just meant how I can generate it to the n number of vectors. Then, I found that you delete your answer. I am really so sorry if I misunderstand you.

    – Maryam
    Nov 12 '18 at 4:48






  • 1





    @akrun I am always proud of your work. It is correct. I really surprised why someone downvoted it.

    – Maryam
    Nov 13 '18 at 9:28

















  • Sorry, it is not clear to me

    – akrun
    Nov 11 '18 at 5:45











  • @MrFlick, thank you so much. This is a typo. I will edit it.

    – Maryam
    Nov 11 '18 at 5:48











  • @akrun Thank you so much for your comment and answer. I just meant how I can generate it to the n number of vectors. Then, I found that you delete your answer. I am really so sorry if I misunderstand you.

    – Maryam
    Nov 12 '18 at 4:48






  • 1





    @akrun I am always proud of your work. It is correct. I really surprised why someone downvoted it.

    – Maryam
    Nov 13 '18 at 9:28
















Sorry, it is not clear to me

– akrun
Nov 11 '18 at 5:45





Sorry, it is not clear to me

– akrun
Nov 11 '18 at 5:45













@MrFlick, thank you so much. This is a typo. I will edit it.

– Maryam
Nov 11 '18 at 5:48





@MrFlick, thank you so much. This is a typo. I will edit it.

– Maryam
Nov 11 '18 at 5:48













@akrun Thank you so much for your comment and answer. I just meant how I can generate it to the n number of vectors. Then, I found that you delete your answer. I am really so sorry if I misunderstand you.

– Maryam
Nov 12 '18 at 4:48





@akrun Thank you so much for your comment and answer. I just meant how I can generate it to the n number of vectors. Then, I found that you delete your answer. I am really so sorry if I misunderstand you.

– Maryam
Nov 12 '18 at 4:48




1




1





@akrun I am always proud of your work. It is correct. I really surprised why someone downvoted it.

– Maryam
Nov 13 '18 at 9:28





@akrun I am always proud of your work. It is correct. I really surprised why someone downvoted it.

– Maryam
Nov 13 '18 at 9:28












2 Answers
2






active

oldest

votes


















0














We can use rep to split the list into a list of lists



lst <- split(x, rep(paste0("x_sub", 1:3), 3:1))


and extract the nested list with the name



lst[["x_sub1"]]


It is better not to create multiple objects in the global environment. But, if it is needed, then use list2env



list2env(lst, envir = .GlobalEnv)
x_sub1
#$x1
#[1] 1 2 3

#$x2
#[1] 1 4 3

#$x3
#[1] 3 4 6





share|improve this answer























  • Thank you so much for your answer. So what about for n vectors. Can I use lst <- split(x, rep(paste0("x_sub", 1:n), n:1)). Also, I have edited my question, if you would like to have a look. Thank you again.

    – Maryam
    Nov 11 '18 at 5:42











  • @Maryam In the question, the input is a list 'x'

    – akrun
    Nov 11 '18 at 5:43


















1














x <- list(x1=c(1,2,3), x2=c(1,4,3), x3=c(3,4,6),
x4=c(4,8,4), x5=c(4,33,4),
x6=c(9,6,7))


You can try the function split() to partition your list x by certain format. I don't understand what you mean for n, k, J, so you need to define them by yourself. I just simply set J as 1:3.



J <- 1:3
breaks <- rep(J, rev(J)) # [1] 1 1 1 2 2 3
y <- split(x, f = breaks)
names(y) <- paste0("x_sub", J)
list2env(y, envir = .GlobalEnv)

x_sub1
x_sub2
x_sub3





share|improve this answer























  • Thank you so much for your answer. n,k, and J are just numbers that help me to define the number of my list and their number of vectors.

    – Maryam
    Nov 11 '18 at 8:42










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246123%2fhow-to-splitting-a-list-of-vectors-to-small-lists-in-decreasing-order-in-r%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














We can use rep to split the list into a list of lists



lst <- split(x, rep(paste0("x_sub", 1:3), 3:1))


and extract the nested list with the name



lst[["x_sub1"]]


It is better not to create multiple objects in the global environment. But, if it is needed, then use list2env



list2env(lst, envir = .GlobalEnv)
x_sub1
#$x1
#[1] 1 2 3

#$x2
#[1] 1 4 3

#$x3
#[1] 3 4 6





share|improve this answer























  • Thank you so much for your answer. So what about for n vectors. Can I use lst <- split(x, rep(paste0("x_sub", 1:n), n:1)). Also, I have edited my question, if you would like to have a look. Thank you again.

    – Maryam
    Nov 11 '18 at 5:42











  • @Maryam In the question, the input is a list 'x'

    – akrun
    Nov 11 '18 at 5:43















0














We can use rep to split the list into a list of lists



lst <- split(x, rep(paste0("x_sub", 1:3), 3:1))


and extract the nested list with the name



lst[["x_sub1"]]


It is better not to create multiple objects in the global environment. But, if it is needed, then use list2env



list2env(lst, envir = .GlobalEnv)
x_sub1
#$x1
#[1] 1 2 3

#$x2
#[1] 1 4 3

#$x3
#[1] 3 4 6





share|improve this answer























  • Thank you so much for your answer. So what about for n vectors. Can I use lst <- split(x, rep(paste0("x_sub", 1:n), n:1)). Also, I have edited my question, if you would like to have a look. Thank you again.

    – Maryam
    Nov 11 '18 at 5:42











  • @Maryam In the question, the input is a list 'x'

    – akrun
    Nov 11 '18 at 5:43













0












0








0







We can use rep to split the list into a list of lists



lst <- split(x, rep(paste0("x_sub", 1:3), 3:1))


and extract the nested list with the name



lst[["x_sub1"]]


It is better not to create multiple objects in the global environment. But, if it is needed, then use list2env



list2env(lst, envir = .GlobalEnv)
x_sub1
#$x1
#[1] 1 2 3

#$x2
#[1] 1 4 3

#$x3
#[1] 3 4 6





share|improve this answer













We can use rep to split the list into a list of lists



lst <- split(x, rep(paste0("x_sub", 1:3), 3:1))


and extract the nested list with the name



lst[["x_sub1"]]


It is better not to create multiple objects in the global environment. But, if it is needed, then use list2env



list2env(lst, envir = .GlobalEnv)
x_sub1
#$x1
#[1] 1 2 3

#$x2
#[1] 1 4 3

#$x3
#[1] 3 4 6






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 '18 at 5:35









akrunakrun

402k13194266




402k13194266












  • Thank you so much for your answer. So what about for n vectors. Can I use lst <- split(x, rep(paste0("x_sub", 1:n), n:1)). Also, I have edited my question, if you would like to have a look. Thank you again.

    – Maryam
    Nov 11 '18 at 5:42











  • @Maryam In the question, the input is a list 'x'

    – akrun
    Nov 11 '18 at 5:43

















  • Thank you so much for your answer. So what about for n vectors. Can I use lst <- split(x, rep(paste0("x_sub", 1:n), n:1)). Also, I have edited my question, if you would like to have a look. Thank you again.

    – Maryam
    Nov 11 '18 at 5:42











  • @Maryam In the question, the input is a list 'x'

    – akrun
    Nov 11 '18 at 5:43
















Thank you so much for your answer. So what about for n vectors. Can I use lst <- split(x, rep(paste0("x_sub", 1:n), n:1)). Also, I have edited my question, if you would like to have a look. Thank you again.

– Maryam
Nov 11 '18 at 5:42





Thank you so much for your answer. So what about for n vectors. Can I use lst <- split(x, rep(paste0("x_sub", 1:n), n:1)). Also, I have edited my question, if you would like to have a look. Thank you again.

– Maryam
Nov 11 '18 at 5:42













@Maryam In the question, the input is a list 'x'

– akrun
Nov 11 '18 at 5:43





@Maryam In the question, the input is a list 'x'

– akrun
Nov 11 '18 at 5:43













1














x <- list(x1=c(1,2,3), x2=c(1,4,3), x3=c(3,4,6),
x4=c(4,8,4), x5=c(4,33,4),
x6=c(9,6,7))


You can try the function split() to partition your list x by certain format. I don't understand what you mean for n, k, J, so you need to define them by yourself. I just simply set J as 1:3.



J <- 1:3
breaks <- rep(J, rev(J)) # [1] 1 1 1 2 2 3
y <- split(x, f = breaks)
names(y) <- paste0("x_sub", J)
list2env(y, envir = .GlobalEnv)

x_sub1
x_sub2
x_sub3





share|improve this answer























  • Thank you so much for your answer. n,k, and J are just numbers that help me to define the number of my list and their number of vectors.

    – Maryam
    Nov 11 '18 at 8:42















1














x <- list(x1=c(1,2,3), x2=c(1,4,3), x3=c(3,4,6),
x4=c(4,8,4), x5=c(4,33,4),
x6=c(9,6,7))


You can try the function split() to partition your list x by certain format. I don't understand what you mean for n, k, J, so you need to define them by yourself. I just simply set J as 1:3.



J <- 1:3
breaks <- rep(J, rev(J)) # [1] 1 1 1 2 2 3
y <- split(x, f = breaks)
names(y) <- paste0("x_sub", J)
list2env(y, envir = .GlobalEnv)

x_sub1
x_sub2
x_sub3





share|improve this answer























  • Thank you so much for your answer. n,k, and J are just numbers that help me to define the number of my list and their number of vectors.

    – Maryam
    Nov 11 '18 at 8:42













1












1








1







x <- list(x1=c(1,2,3), x2=c(1,4,3), x3=c(3,4,6),
x4=c(4,8,4), x5=c(4,33,4),
x6=c(9,6,7))


You can try the function split() to partition your list x by certain format. I don't understand what you mean for n, k, J, so you need to define them by yourself. I just simply set J as 1:3.



J <- 1:3
breaks <- rep(J, rev(J)) # [1] 1 1 1 2 2 3
y <- split(x, f = breaks)
names(y) <- paste0("x_sub", J)
list2env(y, envir = .GlobalEnv)

x_sub1
x_sub2
x_sub3





share|improve this answer













x <- list(x1=c(1,2,3), x2=c(1,4,3), x3=c(3,4,6),
x4=c(4,8,4), x5=c(4,33,4),
x6=c(9,6,7))


You can try the function split() to partition your list x by certain format. I don't understand what you mean for n, k, J, so you need to define them by yourself. I just simply set J as 1:3.



J <- 1:3
breaks <- rep(J, rev(J)) # [1] 1 1 1 2 2 3
y <- split(x, f = breaks)
names(y) <- paste0("x_sub", J)
list2env(y, envir = .GlobalEnv)

x_sub1
x_sub2
x_sub3






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 '18 at 7:12









Darren TsaiDarren Tsai

1,8601323




1,8601323












  • Thank you so much for your answer. n,k, and J are just numbers that help me to define the number of my list and their number of vectors.

    – Maryam
    Nov 11 '18 at 8:42

















  • Thank you so much for your answer. n,k, and J are just numbers that help me to define the number of my list and their number of vectors.

    – Maryam
    Nov 11 '18 at 8:42
















Thank you so much for your answer. n,k, and J are just numbers that help me to define the number of my list and their number of vectors.

– Maryam
Nov 11 '18 at 8:42





Thank you so much for your answer. n,k, and J are just numbers that help me to define the number of my list and their number of vectors.

– Maryam
Nov 11 '18 at 8:42

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246123%2fhow-to-splitting-a-list-of-vectors-to-small-lists-in-decreasing-order-in-r%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế