How to select multiple lists elements at once by pluck?

mm <- list(a = list(1:2), b = list(3:4), d = list(5:6))
nn <- list(a = list(7:8), b = list(9:10), d = list(11:12))
kk <- list(mm, nn)

kk |> map(pluck(2))
[[1]]
[[1]][[1]]
[1] 3 4

[[2]]
[[2]][[1]]
[1] 9 10

If we want to pluck arbitrary list element 2, 3 or more at once with map, like pluck(2:n) didn't work,

or paste("pluck(., ", seq(2:n), ")", collapse = ", ", sep = "") didn't work too,

or paste("pluck(., ", c(2, 3, 5, n), ")", collapse = ", ", sep = "") didn't work too,

how can we do?

We hope the outputs like this

#> List of 2
#>  $ :List of 2
#>   ..$ : int [1:2] 3 4
#>   ..$ : int [1:2] 5 6
#>  $ :List of 2
#>   ..$ : int [1:2] 9 10
#>   ..$ : int [1:2] 11 12
......

Or any other solutions?

Thanks!

E.g. with

library(purrr)

mm <- list(a = list(1:2), b = list(3:4), d = list(5:6))
nn <- list(a = list(7:8), b = list(9:10), d = list(11:12))
kk <- list(mm, nn)

kk1 <- kk |> purrr::map(~c(pluck(.,2),pluck(.,3))) 
str(kk1)
#> List of 2
#>  $ :List of 2
#>   ..$ : int [1:2] 3 4
#>   ..$ : int [1:2] 5 6
#>  $ :List of 2
#>   ..$ : int [1:2] 9 10
#>   ..$ : int [1:2] 11 12
Created on 2021-09-15 by the reprex package (v2.0.0)

?

1 Like

When use paste in the map with purrr expression, it didn't work

mm <- list(a = list(1:2), b = list(3:4), d = list(5:6))
nn <- list(a = list(7:8), b = list(9:10), d = list(11:12))
kk <- list(mm, nn)

kk |> purrr::map(~c(paste("pluck(., ", seq(2:3), ")", collapse = ", ", sep = "")))

How to figure it out?

It works for me but I agree that the output is not very interesting.
Please show us the desired output, because you did not mention that before.

We modified needs clearly at the top post,thanks to your suggestion.

I still don't know exactly what you are looking for.
If it only amounts to selecting an 'arbitrary' number of elements then I would use [ ] as given below.
So no solution with pluck !

Explanation:
I thought that the kk2 solution could work, but I saw that the result was different.
In kk3 I corrected for this.

I hope this works for you. If not, let us know.

library(purrr)

mm <- list(a = list(1:2), b = list(3:4), c = list(5:6))
nn <- list(a = list(7:8), b = list(9:10), c = list(11:12))
kk <- list(mm, nn)

# old version
kk1 <- kk |> purrr::map(~c(pluck(.,2),pluck(.,3))) 
str(kk1)
#> List of 2
#>  $ :List of 2
#>   ..$ : int [1:2] 3 4
#>   ..$ : int [1:2] 5 6
#>  $ :List of 2
#>   ..$ : int [1:2] 9 10
#>   ..$ : int [1:2] 11 12

# newer version but different result
kk2 <- kk |> purrr::map( ~(.[c(2,3)]) )
str(kk2)
#> List of 2
#>  $ :List of 2
#>   ..$ b:List of 1
#>   .. ..$ : int [1:2] 3 4
#>   ..$ c:List of 1
#>   .. ..$ : int [1:2] 5 6
#>  $ :List of 2
#>   ..$ b:List of 1
#>   .. ..$ : int [1:2] 9 10
#>   ..$ c:List of 1
#>   .. ..$ : int [1:2] 11 12

# newest version with same result as old version
kk3 <- kk |> purrr::map( ~(do.call(c,.[c(2,3)])) )
str(kk3)
#> List of 2
#>  $ :List of 2
#>   ..$ b: int [1:2] 3 4
#>   ..$ c: int [1:2] 5 6
#>  $ :List of 2
#>   ..$ b: int [1:2] 9 10
#>   ..$ c: int [1:2] 11 12

# same with three 'plucks'
kk4 <- kk |> purrr::map( ~(do.call(c,.[c(2,3,1)])) )
str(kk4)
#> List of 2
#>  $ :List of 3
#>   ..$ b: int [1:2] 3 4
#>   ..$ c: int [1:2] 5 6
#>   ..$ a: int [1:2] 1 2
#>  $ :List of 3
#>   ..$ b: int [1:2] 9 10
#>   ..$ c: int [1:2] 11 12
#>   ..$ a: int [1:2] 7 8
Created on 2021-09-17 by the reprex package (v2.0.0)
1 Like

map(kk, ~.[2:3])

or

map(kk, magrittr::extract, 2:3)

I don't understand what you're trying to do with the paste here:

kk |> purrr::map(~c(paste("pluck(., ", seq(2:3), ")", collapse = ", ", sep = "")))

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.