purrr iteration of list of named lists.

Just wondered if anyone has tried to work out iterating list of named lists using purr functions.
An example will be a sw_people from repurrrsive package, wth a vector containing a number of column names, and iterating those "columns" and putting them into a tibble...

I understand one could turn transposed sw_people into a tibble and subset needed columns, but I was just wondering if there is a way to do it in a fashion similar to sw_people %>% map("name")...

Any comments will be greatly appreciated!!

Hi @kokad60 , yeah, I would use purrr::imap() for that. If you use it this way %>% imap(~...), then the .x variable will contain the value, and the .y will contain the name of that item.

I probably didn't provide enough on my original post. I didnt mean to imply it's the name of columns I wanted. What I was planning to do was to create a list of column names, c2 <- c("names", "height", "films", ..), and select from the list of named lists, sw_people ( from repurrrsive package ). Selecting a single column is easily done with sw_people %>% map("name"), but selecting more than one column will result in "recursive indexing failed at level 2"...

sw_people is structured as follows:

> sw_people[1:2]
[[1]]
[[1]]$name
[1] "Luke Skywalker"

[[1]]$height
[1] "172"

[[1]]$films
[1] "http://swapi.co/api/films/6/" "http://swapi.co/api/films/3/" "http://swapi.co/api/films/2/" "http://swapi.co/api/films/1/"
[5] "http://swapi.co/api/films/7/"

[[1]]$mass
140 

[[1]]$species
human

[[2]]
[[2]]$name
[1] "C-3PO"

[[2]]$height
[1] "167"

[[2]]$films
[1] "http://swapi.co/api/films/5/" "http://swapi.co/api/films/4/" "http://swapi.co/api/films/6/" "http://swapi.co/api/films/3/"
[5] "http://swapi.co/api/films/2/" "http://swapi.co/api/films/1/"

[[2]]$mass 
130

[[2]]$species
droid


And what I wanted to do was what sw_people %>% transpose() would produce. 
If I have nms <- c("name", "height", "films"), the result will be,

name = list("Luke Skywalker", "C3PO", "Chewie"...)
height = list(140, 145, 350, ...) 
films = list(list(...), list(...), list(...), ...), 

all enclosed in a single list or a tibble.  I hope this is a bit more clear.

My initial thought was to do the following:

nms <- c("name", "height", "films")
sw_people %>% map( ~ .x[[.y]], nms)

Then why not just transpose() and select the names of interest?

transpose(sw_people)[nms]

Side note, the package rlist might be of interest for this kind of tasks.

Seems to work :

library(repurrrsive)

(nms <- c("name", "height", "films"))

map_dfr(sw_people,
        ~{outerx <- .x
        map_dfc(nms,
                ~
                  {
                    enframe(outerx[[.]],
                            name=NULL,
                            value=.)
                  }
        )
        }
)

I think it's very close, but if nms happens to include a column that may be empty for some rows, then the enframe breaks, because the other columns won't be able to "cycle" to match the 0 length... Are there a way to check if a column contains more than one element?

(nms <- c("name", "height", "species"))

map_dfr(sw_people,
~{outerx <- .x
map_dfc(nms,
~
{
enframe(outerx[[.]],
name=NULL,
value=.)
}
)
}
)

Simply transposing the list and turning it into a tibble will result in all columns becoming list types, even the atomic values.