how to slice lists in a tibble?

Hello,

Consider this simple example

# A tibble: 2 x 1
  mylist    
  <list>    
1 <list [3]>
2 <list [4]>

tibble(mylist = list(list('a', 'b', 'c'),
                     list('aa', 'bb', 'cc', 'dd'))) %>% 
mutate(t = map(mylist, ~.x[2,length(.x)]))

Error: Problem with `mutate()` input `t`.
x incorrect number of dimensions
i Input `t` is `map(mylist, ~.x[2, length(.x)])`.
Run `rlang::last_error()` to see where the error occurred.

I am trying to slice the list mylist from the second element to the last one. I do not understand why my syntax does not work.

Any ideas?
Thanks!

You used a comma instead of a colon when subsetting the list. Try this

tibble(mylist = list(list('a', 'b', 'c'),
                     list('aa', 'bb', 'cc', 'dd'))) %>% 
  mutate(t = map(mylist, ~.x[2:length(.x)]))
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.