I've been refactoring some old non-tidyverse code and have been introducing list columns as they make it a lot easier to work with. Thanks to the posts on here for inspiring me and these slides for teaching me how to work with them!
One annoyance I have hit is that when I want to extract a single row out of a tibble, I have to unlist any list columns in order to fetch their value. This makes sense when I am using
filter and there is the potential that I may get additional 0-many rows, so I was thinking that there is space for a
single function that would give me a simple named list of the column items with any list columns de-listed.
Let's say I have a simple tibble, that's really like a hashmap or dictionary:
library(tidyverse) listOfTibbles <- list(tibble(a = c(1,2,3), b = c(2, 3, 4)), tibble(), tibble()) tibbleWithListColumns <- tibble(key = c("a", "b", "c"), value = listOfTibbles) tibbleWithListColumns # A tibble: 3 x 2 key value <chr> <list> 1 a <tibble [3 x 2]> 2 b <tibble [0 x 0]> 3 c <tibble [0 x 0]>
I'd want to be able to use '
single' rather than filter and have it return:
b <- tibbleWithListColumns %>% single(key == "a") b $key  "a" $value # A tibble: 3 x 2 a b <dbl> <dbl> 1 1 2 2 2 3 3 3 4
whereas filter understandably returns $value as a one element list
tibbleWithListColumns %>% filter(key == "a") %>% .$value [] # <------ A one element list # A tibble: 3 x 2 a b <dbl> <dbl> 1 1 2 2 2 3 3 3 4
I know a lot of the time this shouldn't be needed with the use of pmap to map things by row, but in my case it would have been useful and would have prevented [] everywhere. The
single function would throw an error if zero or > 1 items came back, much like
Single in C#.