How to splitting a list of vectors to small lists in decreasing order in r
up vote
2
down vote
favorite
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
add a comment |
up vote
2
down vote
favorite
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
Sorry, it is not clear to me
– akrun
Nov 11 at 5:45
@MrFlick, thank you so much. This is a typo. I will edit it.
– Maryam
Nov 11 at 5:48
@akrun Thank you so much for your comment and answer. I just meant how I can generate it to then
number of vectors. Then, I found that you delete your answer. I am really so sorry if I misunderstand you.
– Maryam
Nov 12 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 at 9:28
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
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
r
edited Nov 11 at 5:47
asked Nov 11 at 5:33
Maryam
18211
18211
Sorry, it is not clear to me
– akrun
Nov 11 at 5:45
@MrFlick, thank you so much. This is a typo. I will edit it.
– Maryam
Nov 11 at 5:48
@akrun Thank you so much for your comment and answer. I just meant how I can generate it to then
number of vectors. Then, I found that you delete your answer. I am really so sorry if I misunderstand you.
– Maryam
Nov 12 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 at 9:28
add a comment |
Sorry, it is not clear to me
– akrun
Nov 11 at 5:45
@MrFlick, thank you so much. This is a typo. I will edit it.
– Maryam
Nov 11 at 5:48
@akrun Thank you so much for your comment and answer. I just meant how I can generate it to then
number of vectors. Then, I found that you delete your answer. I am really so sorry if I misunderstand you.
– Maryam
Nov 12 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 at 9:28
Sorry, it is not clear to me
– akrun
Nov 11 at 5:45
Sorry, it is not clear to me
– akrun
Nov 11 at 5:45
@MrFlick, thank you so much. This is a typo. I will edit it.
– Maryam
Nov 11 at 5:48
@MrFlick, thank you so much. This is a typo. I will edit it.
– Maryam
Nov 11 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 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 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 at 9:28
@akrun I am always proud of your work. It is correct. I really surprised why someone downvoted it.
– Maryam
Nov 13 at 9:28
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
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
Thank you so much for your answer. So what about forn
vectors. Can I uselst <- 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 at 5:42
@Maryam In the question, the input is alist
'x'
– akrun
Nov 11 at 5:43
add a comment |
up vote
1
down vote
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
Thank you so much for your answer.n
,k
, andJ
are just numbers that help me to define the number of my list and their number of vectors.
– Maryam
Nov 11 at 8:42
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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
Thank you so much for your answer. So what about forn
vectors. Can I uselst <- 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 at 5:42
@Maryam In the question, the input is alist
'x'
– akrun
Nov 11 at 5:43
add a comment |
up vote
0
down vote
accepted
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
Thank you so much for your answer. So what about forn
vectors. Can I uselst <- 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 at 5:42
@Maryam In the question, the input is alist
'x'
– akrun
Nov 11 at 5:43
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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
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
answered Nov 11 at 5:35
akrun
392k13181253
392k13181253
Thank you so much for your answer. So what about forn
vectors. Can I uselst <- 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 at 5:42
@Maryam In the question, the input is alist
'x'
– akrun
Nov 11 at 5:43
add a comment |
Thank you so much for your answer. So what about forn
vectors. Can I uselst <- 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 at 5:42
@Maryam In the question, the input is alist
'x'
– akrun
Nov 11 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 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 at 5:42
@Maryam In the question, the input is a
list
'x'– akrun
Nov 11 at 5:43
@Maryam In the question, the input is a
list
'x'– akrun
Nov 11 at 5:43
add a comment |
up vote
1
down vote
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
Thank you so much for your answer.n
,k
, andJ
are just numbers that help me to define the number of my list and their number of vectors.
– Maryam
Nov 11 at 8:42
add a comment |
up vote
1
down vote
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
Thank you so much for your answer.n
,k
, andJ
are just numbers that help me to define the number of my list and their number of vectors.
– Maryam
Nov 11 at 8:42
add a comment |
up vote
1
down vote
up vote
1
down vote
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
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
answered Nov 11 at 7:12
Darren Tsai
776116
776116
Thank you so much for your answer.n
,k
, andJ
are just numbers that help me to define the number of my list and their number of vectors.
– Maryam
Nov 11 at 8:42
add a comment |
Thank you so much for your answer.n
,k
, andJ
are just numbers that help me to define the number of my list and their number of vectors.
– Maryam
Nov 11 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 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 at 8:42
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Sorry, it is not clear to me
– akrun
Nov 11 at 5:45
@MrFlick, thank you so much. This is a typo. I will edit it.
– Maryam
Nov 11 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 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 at 9:28