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")...
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.
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?