简体   繁体   中英

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.

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(xy, function(x) x[1])
#[[1]]
#[1] -0.5604756
#
#[[2]]
#[1] 1.224082

or

lapply(xy, "[[", 1)

Update

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. So your list0 will correspond to lst[[1]] , list1 to lst[[2]] and so on.

The stopifnot(...) line checks that xy[[1]] and xy[[2]] have the same number of elements.

data.table way of solving this.

Gotta say that I REALLY LOVE data.table, a simple transpose would solve this.

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:

    for (i in 1:10){
  eval(parse(text=paste0('list.',i,'<-unlist(transpose(xy)[',i,'])')))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM