[英]How to splitting a list of vectors to small lists in decreasing order in 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.
我需要按降序创建这些列表。
Suppose I have n
variables.假设我有
n
变量。 Then, k = n:2
.然后,
k = n:2
。 My function will return me a list of n(n-1)/2
vectors.我的函数将返回一个包含
n(n-1)/2
向量的列表。 Then, the number of the new sub-lists is n - 1
.那么,新的子列表的数量是
n - 1
。 Hence, I need to have n-1
different lists.因此,我需要有
n-1
不同的列表。 The number of the vectors in each list is J = 1:k-1
.每个列表中的向量数为
J = 1:k-1
。
If n=4
, then, k=4, 3, 2
, then, J=3,2,1
.如果
n=4
,则k=4, 3, 2
,则J=3,2,1
。 Hence, my function will return me 6 vectors.因此,我的函数将返回 6 个向量。 These vectors should be stored into different lists.
这些向量应该存储到不同的列表中。 The number of the vectors in each list is based on
J
.每个列表中向量的数量基于
J
。 Hence, I will have 3 different lists as follows:因此,我将有 3 个不同的列表,如下所示:
In other words, the returned list (the output of my function) should be split into sub-lists in a decreasing order based on J
.换句话说,返回的列表(我的函数的输出)应该基于
J
以降序拆分为子列表。
My function is very complicated.我的功能很复杂。 Hence, I will provide a list of 6 vectors as the output of my function.
因此,我将提供一个包含 6 个向量的列表作为我的函数的输出。
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?例如,我如何将分割思想应用于
n
向量?
Many thanks for all helps.非常感谢所有帮助。
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.您可以尝试使用
split()
函数按特定格式对列表x
进行分区。 I don't understand what you mean for n
, k
, J
, so you need to define them by yourself.我不明白你对
n
, k
, J
,所以你需要自己定义它们。 I just simply set J
as 1:3
.我只是简单地将
J
设置为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
We can use rep
to split
the list
into a list
of lists
我们可以用
rep
来split
的list
成list
的lists
lst <- split(x, rep(paste0("x_sub", 1:3), 3:1))
and extract the nested list
with the name并使用名称提取嵌套
list
lst[["x_sub1"]]
It is better not to create multiple objects in the global environment.最好不要在全局环境中创建多个对象。 But, if it is needed, then use
list2env
但是,如果需要,则使用
list2env
list2env(lst, envir = .GlobalEnv)
x_sub1
#$x1
#[1] 1 2 3
#$x2
#[1] 1 4 3
#$x3
#[1] 3 4 6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.