Home > Software design >  combine lists nested in a tibble
combine lists nested in a tibble

Time:07-08

I want to count the number of unique values in a table that contains several from...to pairs like below:

tmp <- tribble(
  ~group, ~from, ~to,
       1,     1,  10,
       1,     5,   8,
       1,    15,  20,
       2,     1,  10,
       2,     5,  10,
       2,    15,  18
)

I tried to nest all values in a list for each row (works), but combining these nested lists into one vector and counting the uniques doesn't work as expected.

tmp %>%
  group_by(group) %>%
  rowwise() %>%
  mutate(nrs = list(c(from:to))) %>%
  summarise(n_uni = length(unique(unlist(list(nrs)))))

The desired output looks like this:

tibble(group = c(1, 2),
       n_uni = c(length(unique(unlist(list(tmp$nrs[tmp$group == 1])))),
                 length(unique(unlist(list(tmp$nrs[tmp$group == 2]))))))

# # A tibble: 2 × 2
#    group n_uni
#    <dbl> <int>
#1     1    16
#2     2    14

Any help would be much appreciated!

CodePudding user response:

tmp %>% 
  rowwise() %>% 
  mutate(nrs = list(from:to)) %>% 
  group_by(group) %>% 
  summarise(n_uni = n_distinct(unlist(nrs)))

The issue with OP's approach is that rowwise is the equivalent of a new grouping, that drops the initial group_by step. Thus we've got to group_by after creating nrs in the rowwise step.

  • Related