[英]How to extract elements from list of different vectors and store them in a list
Suppose I have two vectors, as this: 假设我有两个向量,如下所示:
set.seed(123)
x <- rnorm(10, 0, 1)
y <- rnorm(10, 0, 1)
xy <- list(x,y)
Example (Just to clarify this is an example): I would like to select the elements of x
and y
(from xy
) and store them in a new list. 示例 (为了澄清这是一个示例):我想选择
x
和y
的元素(从xy
)并将它们存储在新列表中。
For example, 例如,
> xy
[[1]]
[1] -0.56047565 -0.23017749 1.55870831 0.07050839 0.12928774 1.71506499 0.46091621
[8] -1.26506123 -0.68685285 -0.44566197
[[2]]
[1] 1.2240818 0.3598138 0.4007715 0.1106827 -0.5558411 1.7869131 0.4978505 -1.9666172
[9] 0.7013559 -0.4727914
For the first elements, I can do this: 对于第一个元素,我可以这样做:
list1 <– list(-0.56047565, 1.2240818 ).
However, How can I do this for all the elements? 但是,如何对所有元素执行此操作? That is, how can I select every two elements of the list and store it in new lists.
也就是说,如何选择列表的每两个元素并将其存储在新列表中。 For example,
例如,
list1 <– list(-0.56047565, 1.2240818 ).
list2 <- list(-0.23017749, 0.3598138).
...
...
list10 <– list(-0.44566197, -0.4727914).
Any help, please? 有什么帮助吗?
You can use lapply
: 您可以使用
lapply
:
lapply(xy, function(x) x[1])
#[[1]]
#[1] -0.5604756
#
#[[2]]
#[1] 1.224082
or 要么
lapply(xy, "[[", 1)
To do this for all elements you could do: 为此,您可以对所有元素进行操作:
stopifnot(length(xy[[1]]) == length(xy[[2]]))
lst <- lapply(1:length(xy[[1]]), function(i) lapply(xy, "[[", i));
str(lst);
#List of 10
# $ :List of 2
# ..$ : num -0.56
# ..$ : num 1.22
# $ :List of 2
# ..$ : num -0.23
# ..$ : num 0.36
# $ :List of 2
# ..$ : num 1.56
# ..$ : num 0.401
# $ :List of 2
# ..$ : num 0.0705
# ..$ : num 0.111
# $ :List of 2
# ..$ : num 0.129
# ..$ : num -0.556
# $ :List of 2
# ..$ : num 1.72
# ..$ : num 1.79
# $ :List of 2
# ..$ : num 0.461
# ..$ : num 0.498
# $ :List of 2
# ..$ : num -1.27
# ..$ : num -1.97
# $ :List of 2
# ..$ : num -0.687
# ..$ : num 0.701
# $ :List of 2
# ..$ : num -0.446
# ..$ : num -0.473
This will store pairwise elements from x
and y
in a list
of list
s. 这将从配对元素存储
x
和y
在list
的list
秒。 So your list0
will correspond to lst[[1]]
, list1
to lst[[2]]
and so on. 因此,您的
list0
将对应于lst[[1]]
, list1
于lst[[2]]
,依此类推。
The stopifnot(...)
line checks that xy[[1]]
and xy[[2]]
have the same number of elements. stopifnot(...)
行检查xy[[1]]
和xy[[2]]
具有相同数量的元素。
data.table way of solving this. 解决这个问题的data.table方法。
Gotta say that I REALLY LOVE data.table, a simple transpose would solve this. 要说我真的很喜欢data.table,一个简单的转置就可以解决这个问题。
require(data.table)
head(xy)
[[1]]
[1] -0.56047565 -0.23017749 1.55870831 0.07050839 0.12928774 1.71506499 0.46091621 -1.26506123 -0.68685285 -0.44566197
[[2]]
[1] 1.2240818 0.3598138 0.4007715 0.1106827 -0.5558411 1.7869131 0.4978505 -1.9666172 0.7013559 -0.4727914
transpose(xy)
[[1]]
[1] -0.5604756 1.2240818
[[2]]
[1] -0.2301775 0.3598138
[[3]]
[1] 1.5587083 0.4007715
[[4]]
[1] 0.07050839 0.11068272
[[5]]
[1] 0.1292877 -0.5558411
[[6]]
[1] 1.715065 1.786913
[[7]]
[1] 0.4609162 0.4978505
[[8]]
[1] -1.265061 -1.966617
[[9]]
[1] -0.6868529 0.7013559
[[10]]
[1] -0.4456620 -0.4727914
btw, if you want list 1 to 10 created like you wanted, you could write a easy for loop: 顺便说一句,如果您想要创建想要的列表1至10,则可以编写一个简单的for循环:
for (i in 1:10){
eval(parse(text=paste0('list.',i,'<-unlist(transpose(xy)[',i,'])')))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.